简体   繁体   English

在 Angular 中手动触发变化检测

[英]Triggering change detection manually in Angular

I'm writing an Angular component that has a property Mode(): string .我正在编写一个具有属性Mode(): string的 Angular 组件。

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.问题是在没有浏览器事件的情况下,模板绑定{{Mode}}不会更新。

Is there a way to trigger this change detection manually?有没有办法手动触发这种变化检测?

Try one of these:尝试其中之一:

  • ApplicationRef.tick() - similar to AngularJS's $rootScope.$digest() -- ie, check the full component tree ApplicationRef.tick() - 类似于 AngularJS 的$rootScope.$digest() -- 即检查完整的组件树
  • NgZone.run(callback) - similar to $rootScope.$apply(callback) -- ie, evaluate the callback function inside the Angular zone. NgZone.run(callback) - 类似于$rootScope.$apply(callback) - 即,评估 Angular 区域内的回调函数。 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() -- ie, check only this component and its children ChangeDetectorRef.detectChanges() - 类似于$scope.$digest() -- 即,仅检查此组件及其子组件

You can inject ApplicationRef , NgZone , or ChangeDetectorRef into your component.您可以将ApplicationRefNgZoneChangeDetectorRef注入到您的组件中。

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:我使用了已接受的答案参考并想举个例子,因为 Angular 2 文档非常难以阅读,我希望这更容易:

  1. Import NgZone :导入NgZone

     import { Component, NgZone } from '@angular/core';
  2. Add it to your class constructor将它添加到您的类构造函数

    constructor(public zone: NgZone, ...args){}
  3. Run code with zone.run :使用zone.run运行代码:

     this.zone.run(() => this.donations = donations)

I was able to update it with markForCheck()我能够用markForCheck()更新它

Import ChangeDetectorRef导入 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.这是一个示例,其中 markForCheck() 有效而 detectChanges() 无效。

https://plnkr.co/edit/RfJwHqEVJcMU9ku9XNE7?p=preview 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.编辑:这个例子不再描述问题了:(我相信它可能正在运行一个更新的 Angular 版本,它已被修复。

(Press STOP/RUN to run it again) (按 STOP/RUN 再次运行)

In Angular 2+, try the @Input decorator在 Angular 2+ 中,试试 @Input 装饰器

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.然后在使用子模板的父 html 中,添加带有子变量名称的方括号符号,然后将其设置为等于父变量的名称。 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 装饰器:

@Input()
public childVariable: any

When your parent variable is updated, it should pass the updates to the child component, which will update its html.当您的父变量更新时,它应该将更新传递给子组件,子组件将更新其 html。

Also, to trigger a function in the child component, take a look at ngOnChanges.此外,要触发子组件中的函数,请查看 ngOnChanges。

I'm writing an Angular component that has a property Mode(): string .我正在编写一个具有属性Mode(): string的Angular组件。

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.问题在于,在没有浏览器事件的情况下,模板绑定{{Mode}}不会更新。

Is there a way to trigger this change detection manually?有没有一种方法可以手动触发此更改检测?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular Portal 不会触发 ComponentPortal 的变更检测 - Angular Portal not triggering change detection for ComponentPortal Angular HttpInterceptor 缓存未触发更改检测 - Angular HttpInterceptor Cache Not Triggering Change Detection Angular变化检测未在Router.navigate()上触发 - Angular Change Detection not triggering on Router.navigate() Angular 单击事件处理程序未触发更改检测 - Angular click event handler not triggering change detection Angular 2手动触发ngIf - Angular 2 manually triggering ngIf websocket的可怕性能问题 - 每条消息都会触发Angular变化检测 - Terrible performance issues with websocket - every message triggering Angular change detection 删除和添加类之间动态触发更改检测(Angular 2+) - Remove and add class dynamically triggering change detection in between (Angular 2+) 可观察到的Angular 8 UI不会触发更改检测-ChangeDetectionStrategy设置为默认值 - Angular 8 UI observable not triggering change detection - ChangeDetectionStrategy is set to default 防止 Angular 指令上的 MouseEvent 触发更改检测 - Prevent MouseEvent on Angular directive from triggering change detection Angular 2 单击事件回调而不触发更改检测 - Angular 2 click event callback without triggering change detection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM