I have an (Angular 2) root component called AppComponent that uses another component called Subcomp. App passes an @Input() parameter to Sub. Sub uses this variable for one-way binding in an input field.
Now I...
- Set the parameter's value to some initial value ("start"); this is displayed in the input field as expected.
- Change the text in the input field to something else.
- Click on a button to programmatically reset the value in the AppComponent back to "start".
I then expect the input field to also reset to "start", but instead it keeps displaying the changed text from step 2. Is that correct behavior?
The code:
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');
}
}