gpt4 book ai didi

Angular 2 : focusing input causes 'expression has changed after it was changed' error

转载 作者:太空狗 更新时间:2023-10-29 17:36:07 27 4
gpt4 key购买 nike

在我的 Angular 2 应用程序中,我想要一个输入列表。在其中一个中按 Enter 键将添加一个新的输入并立即关注它。这是一个已经在本网站和 Eric Martinez provided a neat answer to it that accomplishes that with a custom directive 上提出的问题.

他的解决方案基于一个虚拟的整数列表。我在尝试使其适应更现实的场景时遇到困难。我已经 fork 了 Eric 的 plunk,所以你可以 run the code here , 但最重要的文件是这个:

//our root app component
import {Component, Directive, Renderer, ElementRef} from 'angular2/core'

class Person { name: string }

@Directive({
selector : 'input'
})
class MyInput {
constructor(public renderer: Renderer, public elementRef: ElementRef) {}

// It won't work at construction time
ngOnInit() {
this.renderer.invokeElementMethod(
this.elementRef.nativeElement, 'focus', []);
}
}

@Component({
selector: 'my-app',
providers: [],
template: `
<div *ngFor="#input of inputs">
<input
(keydown.enter)="add()"
[(ngModel)]="input.name"
type="text"/>
</div>
`,
directives: [MyInput]
})
export class App {
inputs: Person[] = [{name: 'Alice'}];

add() {
var newPerson = new Person();
newPerson.name = 'Bob';

this.inputs.push(newPerson);
}
}

我的数组 inputs现在是 Person 的列表对象。输入双向绑定(bind)到 name Person 的属性. <input>现在包裹在 <div> 中,正如我预期的那样,稍后我会编写更多标记来显示每个 Person .

进行这些更改后,该示例仅在第一次尝试按 Enter 时有效 - 带有文本 Bob 的新输入按预期出现。但是当我第二次尝试按 Enter 时,出现错误:

angular2.dev.js:23730 Error: Expression 'ngClassUntouched in App@2:6' has changed after it was checked. Previous value: 'true'. Current value: 'false'
at ExpressionChangedAfterItHasBeenCheckedException.BaseException [as constructor] (angular2.dev.js:7587)
at new ExpressionChangedAfterItHasBeenCheckedException (angular2.dev.js:4992)
at ChangeDetector_App_1.AbstractChangeDetector.throwOnChangeError (angular2.dev.js:9989)
at ChangeDetector_App_1.detectChangesInRecordsInternal (viewFactory_App:143)
at ChangeDetector_App_1.AbstractChangeDetector.detectChangesInRecords (angular2.dev.js:9874)
at ChangeDetector_App_1.AbstractChangeDetector.runDetectChanges (angular2.dev.js:9857)
at ChangeDetector_App_0.AbstractChangeDetector._detectChangesContentChildren (angular2.dev.js:9930)
at ChangeDetector_App_0.AbstractChangeDetector.runDetectChanges (angular2.dev.js:9858)
at ChangeDetector_HostApp_0.AbstractChangeDetector._detectChangesInViewChildren (angular2.dev.js:9936)
at ChangeDetector_HostApp_0.AbstractChangeDetector.runDetectChanges (angular2.dev.js:9861)

我该如何解决?

我在 Chrome 中运行示例。我发现使用基于 Beta 12 版 Angular2 的 Eric Martinez 的插件来演示问题最简单,但我在现实世界中遇到相同错误的应用程序目前使用的是 Angular 2.0.0。

最佳答案

Angular2 不喜欢在更改检测回调期间更改模型(就像 ngOnInit() 那样)。调用 ChangeDetectorRef.detectChanges() 应该可以修复它:

class MyInput {
constructor(public renderer: Renderer, public elementRef: ElementRef
,private cdRef:ChangeDetectorRef) {}

// It won't work at construction time
ngOnInit() {
this.renderer.invokeElementMethod(
this.elementRef.nativeElement, 'focus', []);
this.cdRef.detectChanges();
}
}

关于 Angular 2 : focusing input causes 'expression has changed after it was changed' error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40562845/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com