我试图了解Angular中的更改检测和模板更新 . 我其实有点困惑 .
我有一个按钮和一个简单的输入字段 . 单击按钮时,我将输入字段的值更改为“test” . 然后创建一个立即返回的异步函数 . 然后我使用for循环等待大约4秒(用于测试目的) .
-
What I expect is: 输入字段的值立即变为"asynched",因为它是异步调用 .
-
Reality : 输入字段的值在4秒后变为"asynched" .
码
updateField(){
this.textContentMain.title = "test"
this.asyncTestFunction();
for(var i=0;i<3999999999;i++){
}
}
asyncTestFunction() {
this._contentSalesTextConfigService.get(this.contentSalesTextConfig).subscribe(item => {
this.textContentMain.title = "asynced";
})
}
模板
<button (click)="updateField()">Update</button>
<input [ngModel]="textContentMain.title" #titleAccessor="ngModel" name="title" id="title" type="text" >
1 回答
这是执行的流程,这应该清除你所有的疑虑