Angular 角度2-例外:表达式';ngClassUntouched在检查后已更改
完整的错误消息是: angular2.dev.js:23597异常:表达式“ngClassUntouched in” myComponent@7:12'在检查后已更改。以前的值: “真的”。[NgClassIn]中的当前值:“false”在中未触及 MyComponent@7:12] 错误指向此控件:Angular 角度2-例外:表达式';ngClassUntouched在检查后已更改,angular,angular2-forms,Angular,Angular2 Forms,完整的错误消息是: angular2.dev.js:23597异常:表达式“ngClassUntouched in” myComponent@7:12'在检查后已更改。以前的值: “真的”。[NgClassIn]中的当前值:“false”在中未触及 MyComponent@7:12] 错误指向此控件: <input [(ngModel)]="searchTxt" class="searchText" type="text" class="form-control" placeholder
<input [(ngModel)]="searchTxt" class="searchText" type="text"
class="form-control" placeholder="Search all departments & sites"
(keypress)="handleKeyboard($event)" [ngClass]="{noBorder: tags.length >
0}" (keyup)="handleKeyboard($event)" autocomplete="off"/>
更多详细信息:这是动态创建的输入数组:
<div *ngFor="#item of records?.data">
<input #input name="r{{item.id}}" class="focusInputBox" type="text"/>
</div>
有什么建议吗
泰
更新:我注意到angular在searchTxt:class=“form control searchText ng untouched ng valid ng dirty”上添加了几个类
当我设置输入框的焦点时,将ng untouched提升到searchTxt中。setTimeout(…)
中的Wrap focus()这就是导致在不期望更改的时间设置类的原因:
setTimeout(() => {
this.inputs.toArray()[0].nativeElement.focus();
}, 0);
也许您可以将
ChangeDetectorRef
注入组件并调用其detectChanges
方法:
constructor(private cdr: ChangeDetectorRef) {}
ngOnInit() {
this.cdr.detectChanges();
}
此方法的调用可以在另一个地方进行,因为它取决于您加载标记的方式…否:(顺便说一句,没有任何相关内容。检查我的更新。我做了一些研究,看起来变化检测有问题,但我无法理解。更新了我的答案。哇!很有效!!泰!!但是你介意为什么会出现这种情况,以及如何修复解决方案吗?我需要看看
…focus()
是从调用的。它似乎是从Angulars change detection启动的东西调用的。Angulars不喜欢它的change detection更改状态。当更改是由setTimeout()
引起时(由Angulars NgZone修补),那么Angular就可以了。因此我们推迟了更改,以便在Angulars chang检测期间不会发生更改,并使其在setTimeout()中发生
Angular可以在哪里使用。如何加载标签列表?谢谢!这是不完整的信息。请提供更多代码。泰,蒂尔!冈特给了我解决方案,但我也会尝试以你的方式从不同的角度来处理这些问题。
setTimeout(() => {
this.inputs.toArray()[0].nativeElement.focus();
}, 0);
constructor(private cdr: ChangeDetectorRef) {}
ngOnInit() {
this.cdr.detectChanges();
}