【发布时间】:2021-04-07 15:38:04
【问题描述】:
只要输入值发生更改,反应性FormControl 元素就会触发ChangeDetection。我添加了OnPush 策略,但它仍然运行ChangeDetection,
@Component({
selector: 'app-server-input',
templateUrl: './server-input.component.html',
styleUrls: ['./server-input.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
FormControl 指令是触发ChangeDetection 的指令。输入更改时如何停止运行ChangeDetection?
serverName: FormControl = new FormControl();
这是我在 StackBlitz 上的 code。
注意:这里要注意的一点是,如果您单击按钮,则会调用 ngDocheck,因此我使用了事件管理器来停止更改检测。
【问题讨论】:
-
内部实现似乎触发了 OnPush 更改检测。此时,最好改变您尝试实施解决方案的方式。
-
请阅读文章,@Robin Dijkhof 是正确答案:indepth.dev/posts/1131/…
-
你必须明白什么时候没有分配fomcontrol,那么就没有变化检测。
标签: angular angular2-changedetection angular-changedetection