In AngularJS you were able to specify watchers to observe changes in scope variables using the $watch
function of the $scope
. What is the equivalent of watching for variable changes (in, for example, component variables) in Angular?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
If you want to make it 2 way binding, you can use
[(yourVar)]
, but you have to implementyourVarChange
event and call it everytime your variable change.Something like this to track the hero change
and then when your hero get changed, call
this.heroChange.emit(this.hero);
the
[(hero)]
binding will do the rest for yousee example here:
http://plnkr.co/edit/efOGIJ0POh1XQeRZctSx?p=preview
Try this when your application still demands
$parse
,$eval
,$watch
like behavior in Angularhttps://github.com/vinayk406/angular-expression-parser
In Angular 2, change detection is automatic...
$scope.$watch()
and$scope.$digest()
R.I.P.Unfortunately, the Change Detection section of the dev guide is not written yet (there is a placeholder near the bottom of the Architecture Overview page, in section "The Other Stuff").
Here's my understanding of how change detection works:
setTimeout()
inside our components rather than something like$timeout
... becausesetTimeout()
is monkey patched.ChangeDetectorRef
.) These change detectors are created when Angular creates components. They keep track of the state of all of your bindings, for dirty checking. These are, in a sense, similar to the automatic$watches()
that Angular 1 would set up for{{}}
template bindings.Unlike Angular 1, the change detection graph is a directed tree and cannot have cycles (this makes Angular 2 much more performant, as we'll see below).
onPush
change detection strategy on any of your components), every component in the tree is examined once (TTL=1)... from the top, in depth-first order. (Well, if you're in dev mode, change detection runs twice (TTL=2). See ApplicationRef.tick() for more about this.) It performs dirty checking on all of your bindings, using those change detector objects.If the component data you want to watch is a primitive input property (String, boolean, number), you can implement
ngOnChanges()
to be notified of changes.If the input property is a reference type (object, array, etc.), but the reference didn't change (e.g., you added an item to an existing array), you'll need to implement
ngDoCheck()
(see this SO answer for more on this).You should only change the component's properties and/or properties of descendant components (because of the single tree walk implementation -- i.e., unidirectional data flow). Here's a plunker that violates that. Stateful pipes can also trip you up here.
Other references to learn more:
onPush
.This behaviour is now part of the component lifecycle.
A component can implement the ngOnChanges method in the OnChanges interface to get access to input changes.
Example:
If, in addition to automatic two-way binding, you want to call a function when a value changes, you can break the two-way binding shortcut syntax to the more verbose version.
<input [(ngModel)]="yourVar"></input>
is shorthand for
<input [ngModel]="yourVar" (ngModelChange)="yourVar=$event"></input>
(see e.g. http://victorsavkin.com/post/119943127151/angular-2-template-syntax)
You could do something like this:
<input [(ngModel)]="yourVar" (ngModelChange)="changedExtraHandler($event)"></input>
Here is another approach using getter and setter functions for the model.