我将任何事件绑定到组件html页面中的元素上,当元素事件触发.although时,组件会持续检测更改。我尝试构建一个简单的项目,除了角核心库之外,没有依赖。
守则:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
changeDetection:ChangeDetectionStrategy.OnPush
})
export class AppComponent {
title = 'angular-tour-of-heroes';
onClick() {
console.log("click");
}
value(){
console.log("detect fired");
return "value";
}
}
app.component.html code
<button type="button" (click)="onClick()">button</button>
{{value()}}当我单击按钮时,浏览器控制台输出:
点击
探测点火
当我单击按钮并输出‘检测启动’时,甚至没有任何代码调用该方法:ChangeDetectorRef.markForCheck()
谢谢!
我认为在执行changedetectionref.markForCheck()之前,单击按钮时不应该执行value()方法
发布于 2022-12-01 06:56:41
在组件输入()引用更改以外的其他几种情况下,OnPush更改检测器都会被触发,它还会被触发:
https://blog.angular-university.io/onpush-change-detection-how-it-works/
https://stackoverflow.com/questions/74637432
复制相似问题