Angular 4 Notes (Part 4) - 2-Way Data Binding

2-way data binding can use to bind data to properties in page.

1. Create a form to enter data.
Lets create a simple user form in user.component.html

<div>
  <label for="name">Name: </label>
  <input type="text" />
</div>
<br>
<div>
    <label for="age">Age: </label>
    <input type="text" />
</div>
<br>

In order to bind data from template to component and vise versa; we need to use ngModel
To use ngModel we have to import forms module.

2. Go to app.modules.ts and import forms module.



3. Go to user.component.html and perform 2-way data binding

It defines by:
1. [(ngModel)] = 'Name of the property'
2. Need name attribute with the same name of the property

Ex:
<input type="text" [(ngModel)]="name" name="name"/>

When a property is inside a model, use it as bellow:
<div>
    <label for="age">Address Street: </label>
    <input type="text" type="text" [(ngModel)]="address.street" name="street"/>
</div>

This model property names should be same as which is in user.component.ts
  name: string = 'Prageeth';
  age: number;
  address: IAddress;
  hobbies: string[];
  data: any;

So, when you change the data in the fields, it will change the appropriate value in the page as well.




Comments

  1. Casino City NJ – Review and Bonus Code - Dr.MD
    Casino City NJ review, 군포 출장샵 bonus code & 태백 출장샵 latest casino bonus codes. 사천 출장샵 All you need to know about Casino City NJ. We have the latest 경상북도 출장안마 in NJ 광주광역 출장마사지 gambling laws and

    ReplyDelete

Post a Comment

Popular posts from this blog

Deploy Angular 8 app to Azure with Azure DevOps

Apache ActiveMQ 5 with .Net Core 3.1 and C#

Firebase with.Net Core and C#