Sử dụng template variables

Understanding template variables Template variables giúp bạn sử dụng data từ một phần của một template trong một phần khác của template. Sử dụng template variables để thực hiện các tác vụ như phản hồi thông tin nhập của người dùng hoặc tinh chỉnh các biểu mẫu ứng dụng của bạn. Syntax Trong template, bạn

Understanding template variables

Template variables giúp bạn sử dụng data từ một phần của một template trong một phần khác của template. Sử dụng template variables để thực hiện các tác vụ như phản hồi thông tin nhập của người dùng hoặc tinh chỉnh các biểu mẫu ứng dụng của bạn.

Syntax

Trong template, bạn sử dụng ký hiệu # để khai báo một template variable. Biến mẫu sau, #phone, khai báo một biến phone với phần tử <input> làm giá trị của nó.
src/app/app.component.html

<input #phone placeholder="phone number"/>

Tham chiếu đến một template variable ở bất kỳ đâu trong template của component. Tại đây, một <button> ở phía dưới template tham chiếu đến đến biến phone .
src/app/app.component.html

<input #phone placeholder="phone number"/><!-- lots of other elements --><!-- phone refers to the input element; pass its `value` to an event handler --><button type="button"(click)="callPhone(phone.value)">Call</button>

src/app/app.component.ts

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

@Component({
  selector:'app-root',
  templateUrl:'./app.component.html',
  styleUrls:['./app.component.css']})exportclassAppComponent{callPhone(phone: string):void{
      console.log(phone);}}

Output:

Using NgForm with template variables

Hãy cập nhật code vào template như dưới:
src/app/app.component.html

<div class="container"><form #itemForm="ngForm"(ngSubmit)="onSubmit(itemForm)"><label for="name">Name</label><input type="text" id="name"class="form-control" name="name" ngModel required /><label for="name">Email</label><input type="text" id="email"class="form-control" name="email" ngModel required /><button type="submit">Submit</button></form><div [hidden]="!itemForm.form.valid"><p>{{ submitMessage }}</p></div></div>

src/app/app.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
    submitMessage: string = 'Value input valid!';

    onSubmit(itemForm: { value: { name: any; email: any; }; }): void {
      console.log(itemForm.value.name);
      console.log(itemForm.value.email);
    }
}

Output:

Nguồn: viblo.asia

Bài viết liên quan

Sự Khác Nhau Giữa Domain và Hosting Là Gì?

Sự khác nhau giữa domain và hosting là gì? Bài này giải thích ngắn và dễ hiểu nh

Shared Hosting hay VPS Hosting: Lựa chọn nào dành cho bạn?

Bài viết giải thích rõ shared hosting và vps hosting là gì và hướng dẫn chọn lựa

Thay đổi Package Name của Android Studio dể dàng với plugin APR

Nếu bạn đang gặp khó khăn hoặc bế tắc trong việc thay đổi package name trong And

Lỗi không Update Meta_Value Khi thay thế hình ảnh cũ bằng hình ảnh mới trong WordPress

Mã dưới đây hoạt động tốt có 1 lỗi không update được postmeta ” meta_key=