【发布时间】:2017-03-26 13:28:37
【问题描述】:
在我的 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 对象的列表。输入双向绑定到Person 的name 属性。 <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 中运行该示例。我发现使用基于 Angular2 beta 12 版本的 Eric Martinez 的 plunk 最容易演示该问题,但我遇到相同错误的实际应用程序当前使用的是 Angular 2.0.0。
【问题讨论】:
标签: angular typescript angular2-directives