25
loading...
This website collects cookies to deliver better user experience
ngModel
[(ngModel)]="<PROPERTY>"
--- ( also called banana with in a box , not official though)ngModel
is used for two-way data binding.FormsModule
myMarks
in your component ts file which holds a value of 78 in this case.interpolation
. {{ <The_Variable_Name }}
.{{ myMarks }}
{{ }}
double curly braces.myMarks
(variable we just created in TS file) needs to be displayed when the component is displayed and once you change the value in the text box then the corresponding value should be updated in the component TS file variable.model
and html is the template. So when the component is first time loaded or displayed the model value goes and sits in the text box (that's 1) and when you update it in the textbox then the model also gets updated (making it 2). Since we are binding the data we call it two way data binding.ngModel
ngModel
is present inside a different module called FormsModule
. And if we need to use this directive we need to import the same in our module (I will talk about modules in details very soon).import { FormsModule } from '@angular/forms';
FormsModule
FormsModule
<input [(ngModel)]="myMarks" required />
{{ myMarks }}
myMarks
variable we already created earlier in this post (and assigned the value 78 to it) in the corresponding TS file.myMarks
) also gets updated to 95. Since the model gets updated the interpolated value ( myMarks
written inside double curly braces) also gets updated.ngModel
does this big process under the hood so smoothly.