[(ngModel)]="item"
is a shorthand for [ngModel]="item" (ngModelChange)="item = $event"
That means that if you want to add a 2-way bind property to your component, for example
<app-my-control [(myProp)]="value"></app-my-control>
All you need to do in your component is add
@Input()
myProp: string;
// Output prop name must be Input prop name + 'Change'
// Use in your component to write an updated value back out to the parent
@Output()
myPropChange = new EventEmitter<string>();
The @Input
will handle the write ins and to write a new value back out to the parent, just call this.myPropChange.emit("Awesome")
(You can put the emit in a setter for your property if you just want to make sure it is updated every time the value changes.)
You can read a more detailed explanation of how/why it works here.
If you want to use the name ngModel
(because there are extra directives that bind to elements with ngModel
), or this is for a FormControl
element rather than a component (AKA, for use in an ngForm
), then you will need to play with the ControlValueAccessor
. A detailed explanation for making your own FormControl
and why it works can be read here.