How to use Angular’s template reference variables

How to use Angular’s template reference variables

Angular has a mechanism called data binding, and a similar feature is called template reference variables.

You can specify an element in a template as #variable name to use that variable.

The way to write it is as follows.

<element #variable_name></element>

The following is an example of displaying the value entered in a text box below the text box.

app.component.ts

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

@Component({
  selector: 'app-root',
  template:
  `
  <input type='text' #ttt (change)='onChange(ttt.value)'><br>
  <p>{{a}}</p>
  `,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  a:string;
  onChange(str: string){
    this.a = str;
  }
}

It works as follows

Angular のテンプレート参照変数の使い方

It can also be completed only in the view by setting (change)=’0’` as follows.

app.component.ts

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

@Component({
  selector: 'app-root',
  template:
  `
  <input type='text' #ttt (change)='0'><br>
  <p>{{ttt.value}}</p>
  `,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
}

Event binding is used to execute asynchronous processing and update the view.

コメント

Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL