Angular之模版引用变量

A template reference variable is often a reference to a DOM element within a template. It can also be a reference to an Angular component or directive or a web component.

模板引用变量通常用来引用模板中的某个 DOM 元素,也可以引用 Angular 组件、指令、Web Component(自定义元素标签)。

 

store.component.ts

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

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

  name = 'SUV';

  constructor() { }

  ngOnInit() {
  }

}

store.component.html

<!-- 引用DOM元素 -->

<span #title>4S店</span>
<button (click)="print(title.innerHTML);">单击</button>

<input type="tel" #tel value="021-12345678"/>
<button (click)="print(tel.value);">单击</button>

<!-- 引用Angular组件 -->

<app-car #car></app-car>
<button (click)="print(car.name);">单击</button>

car.component.ts

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

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

  name = 'SUV';

  constructor() { }

  ngOnInit() {
  }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值