ChatGPT解决这个技术问题 Extra ChatGPT

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}} doesn't update.

Is there a way to trigger this change detection manually?


L
Lazar Ljubenović

Try one of these:

ApplicationRef.tick() - similar to AngularJS's $rootScope.$digest() -- i.e., check the full component tree

NgZone.run(callback) - similar to $rootScope.$apply(callback) -- i.e., evaluate the callback function inside the Angular zone. I think, but I'm not sure, that this ends up checking the full component tree after executing the callback function.

ChangeDetectorRef.detectChanges() - similar to $scope.$digest() -- i.e., check only this component and its children

You can inject ApplicationRef, NgZone, or ChangeDetectorRef into your component.


Thanks, I opted for the 3rd solution so as to not check everything, since the changes are fairly localized. I should investigate the other options when I have more time. Are there any performance implications with each choice?
+1 for ChangeDetectorRef.detectChanges(). validators were firing before my directive could update an input's value.
ApplicationRef.tick() and ChangeDetectorRef.detectChanges() are still present in 2.0.0 final.
Just thought I'd mention this. These are not static methods, they're instance methods. You're going to need to inject these classes as services.
ApplicationRef.tick() helped solve an issue with FireFox v17 (<20)
s
sites

I used accepted answer reference and would like to put an example, since Angular 2 documentation is very very hard to read, I hope this is easier:

Import NgZone: import { Component, NgZone } from '@angular/core'; Add it to your class constructor constructor(public zone: NgZone, ...args){} Run code with zone.run: this.zone.run(() => this.donations = donations)


where should you put the zone.run code and what exactly is donations?
@suku donations is any property you want to update, so it could be this.foo = bar. zone.run goes wherever you want to update stuff.
I used this solution to force a view update when using document.addEventListener("resume", callback)
N
Nuno Tomas

I was able to update it with markForCheck()

Import ChangeDetectorRef

import { ChangeDetectorRef } from '@angular/core';

Inject and instantiate it

constructor(private ref: ChangeDetectorRef) { 
}

Finally mark change detection to take place

this.ref.markForCheck();

Here's an example where markForCheck() works and detectChanges() don't.

https://plnkr.co/edit/RfJwHqEVJcMU9ku9XNE7?p=preview

EDIT: This example doesn't portray the problem anymore :( I believe it might be running a newer Angular version where it's fixed.

(Press STOP/RUN to run it again)


Goo point for detectChanges() not working. I had noticed the same issue and found that it works if the change detection is no OnPush. Would be good to get an explanation for that...
It seems like detectChanges is actually working in this plunker? Am I missing something?
You should note that ChangeDetectorRef only works in Components
(!) This is a bad example, I am sorry. Example does not show anything. First item just get overwritten. Just adjust timers a bit ...
This example doesn't portrays the problem anymore :( I believe it might be running a newer Angular version where it's fixed.
J
Jeremy Swagger

In Angular 2+, try the @Input decorator

It allows for some nice property binding between parent and child components.

First create a global variable in the parent to hold the object/property that will be passed to the child.

Next create a global variable in the child to hold the object/property passed from the parent.

Then in the parent html, where the child template is used, add square brackets notation with the name of the child variable, then set it equal to the name of the parent variable. Example:

<child-component-template [childVariable] = parentVariable>
</child-component-template>

Finally, where the child property is defined in the child component, add the Input decorator:

@Input()
public childVariable: any

When your parent variable is updated, it should pass the updates to the child component, which will update its html.

Also, to trigger a function in the child component, take a look at ngOnChanges.


NO...thats the problem...if you update in the parent...say for exmaple is updated "by reference" in a static method the child wont pick it up as change detection hasnt occured
I have got the same problem for days now. I try to retrieve data from PHP backend and data does not Update at all. On Microsoft Edge works fine but on other browser it does not. I keep getting the data i first got on start but if data changes it does not get updated on view