AngularJs 1.5 - Component does not support Watchers, what is the work around?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've been upgrading my custom directives to the new component method.I've read that component's does not support watchers. Is this correct? If so how do you detect changes on an object. For a basic example I have custom component myBox which has a child component game with a binding on the game . If there is a change game within the game component how do I show an alert message within the myBox? I understand there is rxJS method is it possible to do this purely in angular? My JS FIDDLE JS FIDDLE
This is the preferred method going forward. The AngularJS strategy of using $watch is not scalable because it is a polling strategy. When the number of $watch listeners reaches around 2000, the UI gets sluggish. The strategy in Angular 2 is to make the framework more reactive and avoid placing $watch on $scope.
Use the $onChanges Life-cycle Hook
With version 1.5.3, AngularJS added the $onChanges life-cycle hook to the $compile service.
From the Docs:
The controller can provide the following methods that act as life-cycle hooks:
$onChanges(changesObj) - Called whenever one-way () or interpolation (@) bindings are updated. The changesObj is a hash whose keys are the names of the bound properties that have changed, and the values are an object of the form { currentValue: ..., previousValue: ... }. Use this hook to trigger updates within a component such as cloning the bound value to prevent accidental mutation of the outer value.
The $onChanges hook is used to react to external changes into the component with one-way bindings. The ng-change directive is used to propogate changes from the ng-model controller outside the component with & bindings.
Use the $doCheck Life-cycle Hook
With version 1.5.8, AngularJS added the $doCheck life-cycle hook to the $compile service.
From the Docs:
The controller can provide the following methods that act as life-cycle hooks:
$doCheck() - Called on each turn of the digest cycle. Provides an opportunity to detect and act on changes. Any actions that you wish to take in response to the changes that you detect must be invoked from this hook; implementing this has no effect on when $onChanges is called. For example, this hook could be useful if you wish to perform a deep equality check, or to check a Date object, changes to which would not be detected by Angular's change detector and thus not trigger $onChanges. This hook is invoked with no arguments; if detecting changes, you must store the previous value(s) for comparison to the current values.
Directives can require the controllers of other directives to enable communication between each other. This can be achieved in a component by providing an object mapping for the require property. The object keys specify the property names under which the required controllers (object values) will be bound to the requiring component's controller.
What about in a situation where you have a Service that's holding state for example. How could I push changes to that Service, and other random components on the page be aware of such a change? Been struggling with tackling this problem lately
var app = angular.module('myApp', ['rx']); app.factory("DataService", function(rx) { var subject = new rx.Subject(); var data = "Initial"; return { set: function set(d){ data = d; subject.onNext(d); }, get: function get() { return data; }, subscribe: function (o) { return subject.subscribe(o); } }; });
$watch object is available inside $scope object, so you need to add $scope inside your controller factory function & then place watcher over the variable.
Note: I know you had converted directive to component, to remove dependency of $scope so that you will get one step closer to Angular2. But it seems like it didn't get removed for this case.
Update
Basically angular 1.5 does added .component method jus differentiate two different functionality. Like component.stands to perform particular behaviby adding selector, where as directive stands to add specific behavior to DOM. Directive is just wrapper method on .directive DDO(Directive Definition Object). Only what you can see is, they had remove link/compile function while using .component method where you had an ability to get angular compiled DOM.
Do use $onChanges/$doCheck lifecycle hook of Angular component lifecycle hook, those will be available after Angular 1.5.3+ version.
$onChanges(changesObj) - Called whenever bindings are updated. The changesObj is a hash whose keys are the names of the bound properties.
$doCheck() - Called on each turn of the digest cycle when binding changes. Provides an opportunity to detect and act on changes.
By using same function inside component will ensure your code to be compatible to move to Angular 2.
回答3:
To anyone interested in my solution, I end up resorting to RXJS Observables, which what you will have to use when you get to Angular 2. Here is a working fiddle for communications between components, it gives me more control on what to watch.
Available in IE11, MutationObserver https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver. You need to inject $element service into controller which semi-breaks DOM/controller separation, but I feel that this is a fundamental exception (ie. flaw) in angularjs. Since hide/show is async, we need on-show callback, that angularjs & angular-bootstrap-tab do not provide. It also requires that u know which specific DOM element u want to observe. I used following code for angularjs controller to trigger Highcharts chart reflow on-show.