Search code examples
angularngmodel

Angular two-way binding not woking in template-driven forms


I am trying to read a property from the class in my template-driven form but there's no way of making it work. This is my class:

...
export class IdComponent implements OnInit {
      title : String = "Edit user";
...

And this is my form

<form>
    <input type="text" id="title1" [(ngModel)]="this.title">
    <input type="text" id="title2" value = "{{ this.title }}">
</form>

First input works but second doesn't. What am I doing wrong? Here's an evidence

enter image description here


Solution

  • Your html should look like this:

    <form>
      <input type="text" name="title" [(ngModel)]="title" />
      <input type="text" value="{{ title }}">
    </form>
    

    Or in other words you need to add name attribute to input with [(ngModel)]