Very simple, I have a component containing a string
I need to show this string in an input field.
I thought I need it like this but it doesn't work.
import {Component} from "@angular/core";
@Component({
selector: 'my-component',
template: `
<input type="text" ([ngModel])="myVar" >
`
})
export class MyComponent{
constructor(){
}
myVar: string = "Hello World"
}
[(ngModel)]="myVar"(or[ngModel]="myVar"if it is just 1-way binding)[ngModel]="myVar"doesn't worknameattribute unless you use this input as a template driven formControl (not mentioned in the original OP). For implementing the binding between theinputfield and the component property just the right 'banana-in-a-box' syntax should suffice. See this to learn more on implementing 2-way bindings. Also, like I mentioned,[ngModel]is just 1-way binding - meaning it will populate the value once from the component to the view on initial load.