Angular -- Template Variables

<input type="text" id="item_input #item>

#item is an example of a template variable, used to capture the user input of <input> element.

Template variables help you use data from one part of a template in another part of the template.

  • Syntax: hash (#) character + variable name

A template variable can refer to the following:

Refering value

  • What value is stored in a template variable?

Answer: 

How Angular assigns values to template variablesicon-default.png?t=M4ADhttps://angular.io/guide/template-reference-variables#how-angular-assigns-values-to-template-variables

DOM Element

  • i.e. Declared in an HTML tag
  • Just like the previous example: <input ... #item>
  • In this type of situation, the value is the input HTML element itself.

Component

  • i.e. Declared in a component tag
  • e.g. <app-child #child>
  • Here the #child captures the child component as a whole
  • Say, you want to display the "item" field in the child component:
    • In parent component: <p> {{child.item}} </p> OR
    • <input [value]="child.item">

Variable scope

  • If declared inside *ngIf, *ngFor, then it's scoped inside that structural directive
    • On the other hand, if declared outside, then it can be used inside other stuctural directives

Template input vaiable

  • A way to declare temporary limited scope variables
  • Format: let-XXX

Angular template variablesicon-default.png?t=M4ADhttps://angular.io/guide/template-reference-variables#template-input-variable

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值