我有一个名为 AppComponent 的(Angular 2)根组件,它使用另一个名为 Subcomp 的组件。App 将@Input()
参数传递给 Sub。Sub 使用此变量在输入字段中进行单向绑定。
现在我...
- 将参数的值设置为某个初始值(“start”);这按预期显示在输入字段中。
- 将输入字段中的文本更改为其他内容。
- 单击按钮以编程方式将 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');
}
}