2
我有一个名为AppComponent的(Angular 2)根组件,它使用另一个名为Subcomp的组件。 App将@Input()
参数传递给Sub。 Sub在输入字段中使用此变量进行单向绑定。为什么在这种情况下Angular 2绑定不起作用? (子组件输入字段)
现在我...
- 参数的值设置一些初始值( “开始”);这如预期的那样显示在输入字段中。
- 将输入字段中的文本更改为其他内容。
- 单击一个按钮以编程方式将AppComponent中的值重置为“开始”。
然后我希望输入字段也重置为“开始”,但是它会一直显示步骤2中更改的文本。是否正确的行为?
代码:
class Todo {
constructor(public title: string) {}
}
@Component({
selector: 'subcomp',
directives: [FORM_DIRECTIVES],
template: `New Title: <input type="text" [ngModel]="subtodo.title">`
})
export class Subcomp {
@Input() subtodo: Todo;
}
@Component({
selector: 'my-app',
directives: [Subcomp],
template: `To do: {{todo.title}}<br/>
<subcomp [subtodo]="todo"></subcomp><br/>
<button (click)="update()">Update</button>`
})
export class AppComponent {
todo: Todo = new Todo('start');
update() {
this.todo = new Todo('start');
}
}
OK,这使得即使使用的时候似乎违反直觉的感觉。谢谢! –
当update()创建的'new Todo('xxx')'具有不同的值时,它仍然不会更新输入。而不是当'Todo'' ngModel'绑定到一个简单的字符串字段。对我来说,它仍然看起来像一个错误。 –
@GünterZöchbauer,这个[plunker](http://plnkr.co/edit/B9GyaIeqV4TSIJMUR3Qv?p=preview)显示'新的Todo('start'+ new Date())'确实更新了输入。 (使用Beta.0,如果这很重要,我还没有移除它,因为它似乎有更少的bug。) –