Search code examples
angularstring-interpolation

controller not actualize string interpolation


I got a problem

My hellow.component.html is:

<form>
    <input ng-model="hello" type="text">
</form>
<p>{{hello}}</p>

and hellow.component.ts is:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-hellow',
  templateUrl: './hellow.component.html',
  styleUrls: ['./hellow.component.css']
})
export class HellowComponent implements OnInit {

  hello:string ='';
  constructor() { }

  ngOnInit() {
  }

}

Can you answer
If I write something in input, {{hello}} string interpolation is not actualized by controller. Can you help me with this matter?

also tried with:

<input [(ngModel)]="hello" type="text">

Thank you, a lot.


Solution

  • When using the ngModel within tags, you'll also need to supply a name attribute so that the control can be registered with the parent form under that name. Try this.

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