angular - 反应式表单控件触发更改检测
问题描述
每当输入值更改时,都会触发反应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 上的代码。
注意:这里要注意的一件事是,如果您单击按钮,则会调用 ngDocheck,因此我使用事件管理器来停止更改检测。
解决方案
ngDoCheck
总是运行。不管你使用什么策略。请参阅https://indepth.dev/posts/1131/if-you-think-ngdocheck-means-your-component-is-being-checked-read-this-article以及我们为什么需要`ngDoCheck`
我看不出您有任何理由想知道何时、如何运行更改检测。如果您想对更改做一些事情,只需在您的输入上使用 setter:
_name: string;
get name(): string{
return this._name;
}
@Input() set name(value: string) {
this._name = value;
this.doUpdateSomething();
}
推荐阅读
- php - 使用 RegExp 从 Whois 中提取信息
- javascript - Date.now() 是否为不同的时区返回不同的值?
- javascript - 在 axios 请求拦截器中发送请求不起作用
- html - 之前设置 p 样式时,div 的 p 下降格式奇怪
- python - 在 Python 网页抓取方面需要帮助
- xamarin - DataTemplate C#中的绑定按钮
- c++ - 如何在 cpp 类中声明 zeromq 套接字
- postgresql - PostgreSQL 周数不正确?
- powershell - 为 50 个用户启用电子邮件转发的 PowerShell 脚本
- reactjs - 无法使用 onChange 方法恢复复选框状态