Questions tagged [angular2-changedetection]
473 questions
832
votes
20 answers
How to detect when an @Input() value changes in Angular?
I have a parent component (CategoryComponent), a child component (videoListComponent) and an ApiService.
I have most of this working fine i.e. each component can access the json api and get its relevant data via observables.
Currently video list…

Jon Catmull
- 11,873
- 5
- 19
- 17
557
votes
31 answers
ExpressionChangedAfterItHasBeenCheckedError Explained
Please explain to me why I keep getting this error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
Obviously, I only get it in dev mode, it doesn't happen on my production build, but it's very annoying and…

Kevin LeStarge
- 8,142
- 4
- 21
- 34
485
votes
4 answers
Triggering change detection manually in Angular
I'm writing an Angular component that has a property Mode(): string.
I would like to be able to set this property programmatically not in response to any event.
The problem is that in the absence of a browser event, a template binding {{Mode}}…

jz87
- 9,199
- 10
- 37
- 42
393
votes
19 answers
@ViewChild in *ngIf
Question
What is the most elegant way to get @ViewChild after corresponding element in template was shown?
Below is an example. Also Plunker available.
Component.template.html:

sinedsem
- 5,413
- 7
- 29
- 46
272
votes
5 answers
What's the difference between markForCheck() and detectChanges()
What is the difference between ChangeDetectorRef.markForCheck() and ChangeDetectorRef.detectChanges()?
I only found information on SO as to the difference between NgZone.run(), but not between these two functions.
For answers with only a reference…

parliament
- 21,544
- 38
- 148
- 238
225
votes
7 answers
What is the Angular equivalent to an AngularJS $watch?
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?

Erwin
- 3,298
- 2
- 15
- 22
223
votes
6 answers
How to force a component's re-rendering in Angular 2?
How to force a component's re-rendering in Angular 2?
For debug purposes working with Redux i'd like to force a component to re-render it's view, is that possible?

born2net
- 24,129
- 22
- 65
- 104
87
votes
11 answers
ngIf - Expression has changed after it was checked
I have a simple scenario, but just can't get it working!
In my view I display some text in a box with limited height.
The text is being fetched from the server, so the view updates when the text comes in.
Now I have an 'expand' button that has a…

naomi
- 2,583
- 2
- 22
- 39
70
votes
6 answers
Trigger update of component view from service - No Provider for ChangeDetectorRef
I would like to udpate my application view, triggered by events from service.
One of my services injects the ChangeDetectorRef. Compilation works, but I am getting an error in the browser when the App is bootstrapped: No provider for…

EricJ
- 912
- 1
- 6
- 12
55
votes
11 answers
Angular2: *ngFor does not update when array is updated
I have an array of objects (arr). In one of my component's inputs, in the (change) method, I modify the attribute of one of the objects but in the view (*ngFor) nothing changes. I read that Angular’s change detection doesn't check the contents of…

Roland Rácz
- 2,879
- 3
- 18
- 44
42
votes
5 answers
Angular2 - Expression has changed after it was checked - Binding to div width with resize events
I have done some reading and investigation on this error, but not sure what the correct answer is for my situation. I understand that in dev mode, change detection runs twice, but I am reluctant to use enableProdMode() to mask the issue.
Here is a…

Anthony Day
- 537
- 2
- 5
- 7
39
votes
2 answers
Angular2 zone.run() vs ChangeDetectorRef.detectChanges()
Say I have a function noificationHandler() in my service.ts that is outside of angular's context.
noificationHandler() is invoked by a third party and noificationHandler() basically consumes an array and emits the array to components which have…

Ajey
- 7,924
- 12
- 62
- 86
33
votes
2 answers
Why do I need to call detectChanges() with the default change detection strategy?
I am working on an Angular 4 app, but I am having a problem inasmuch as I am having to call this.changeDetectorRef.detectChanges(); to update the view when the model changes. For example, I have this code which is for…

serlingpa
- 12,024
- 24
- 80
- 130
30
votes
2 answers
Angular 2 Component listen to change in service
I've a simple question about change detection.
I have a component and a (global) service with a boolean inside.
How can I make the component listen to that boolean and execute a function if that boolean changes?
Thanks!

Han Che
- 8,239
- 19
- 70
- 116
25
votes
5 answers
How to re-trigger all pure pipes on all component tree in Angular 2
I have pure pipe TranslatePipe that translates phrases using LocaleService that has locale$: Observable current locale. I also have ChangeDetectionStrategy.OnPush enabled for all my components including AppComponent. Now, how can I reload…

Ivan Zyranau
- 879
- 2
- 14
- 33