这是另一个“视图不变”问题
我有一个组件,当我选择一个名称时需要更新它。但是,我不知道为什么,我必须在模型更改之前选择 2 倍的名称。这就像我第一次选择模型没有更新。
使用带有 materializeCSS 的 angular 2 4.0.0(来自 materialize 的自动完成)
那么,我想在选择后直接显示该按钮。
这部分的 HTML 模板:
<form>
<i class="material-icons prefix">search</i>
<input id="search" type="text" name="autoComplete" materialize="autocomplete" [materializeParams]="[autocompleteInit]">
</form>
<div *ngIf="CVSelected.getCV()">
<button type="button" [routerLink]="['/dashboard/cv']" class="waves-effect waves-light btn">Passer à l'édition</button>
</div>
自动完成初始化:
autocompleteInit: any = {
data: {Some_data_here},
onAutocomplete: (str: string) => {
this.dataService.GetFromString(str).subscribe(value => {
console.log(value);
this.CVSelected.setCV(value[0]); // This makes the button appears on my template with a get
});
this.changement.detectChanges(); // Tried this and other functions that does the same but doesn't work
},
};
有人遇到过这个问题吗?如果是,一些解决方案?
先感谢您
在这种情况下,最可能的原因是角度区域之外的执行代码。为了解决这个问题,我建议您在 angular zone 内运行代码,例如:
constructor(private zone: NgZone) { }
autocompleteInit: any = {
data: {},
onAutocomplete: (str: string) => {
zone.run(() => {
this.dataService.GetFromString(str).subscribe(value => {
console.log(value);
this.CVSelected.setCV(value[0]); // This makes the button appears on my template with a get
});
});
},
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句