Angular7 - 模板(Templates)

首页 / Angular7入门教程 / Angular7 - 模板(Templates)

Angular 7使用<ng-template>作为标签,而不是Angular2中使用的<template>。 自Angular 4发布以来一直使用<ng-template>,而较早的版本(即Angular 2)出于相同目的使用<template>。 从Angular 4开始它开始使用<ng-template>而不是<template>的原因是因为<template>标签和html <template>标准标签之间存在名称冲突。 它将完全淘汰, 这是Angular 4版本中的主要更改之一。

现在让我们将模板与 if else条件一起使用,并查看输出。

app.component.html

<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
   <h1>Welcome to {{title}}.</h1>
</div>

<div> Months :
   <select (change)="changemonths($event)" name="month">
      <option *ngFor="let i of months">{{i}}</option>
   </select>
</div>
<br/>

<div>
   <span *ngIf="isavailable;then condition1 else condition2">
      Condition is valid.
   </span>
   <ng-template #condition1>Condition is valid from template</ng-template>
   <ng-template #condition2>Condition is invalid from template</ng-template>
</div>
<button (click)="myClickFunction($event)">Click Me</button>

对于Span标签,我们添加了 if 语句和 else 条件,并将调用模板condition1,否则调用condition2。

模板被称为如下-

<ng-template #condition1>Condition is valid from template</ng-template> 
<ng-template #condition2>Condition is invalid from template</ng-template>

如果条件为true,则调用 condition1 模板,否则调用 condition2 。

无涯教程网

app.component.ts

import { Component } from '@angular/core';
@Component({ 
   selector: 'app-root', 
   templateUrl: './app.component.html', 
   styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
   title='Angular 7'; 
   
   //declared array of months. 
   months=["January", "February", "March", "April", "May", "June", "July", 
      "August", "September", "October", "November", "December"]; 
   isavailable=false; //variable is set to true
   
   myClickFunction(event) { 
      //just added console.log which will display the event details in browser on click of the button. 
      alert("Button is clicked"); 
      console.log(event); 
   }
   changemonths(event) { 
      alert("Changed month from the Dropdown"); 
   } 
}

浏览器中的输出如下-

Condition Invalid

变量 isavailable 为false,因此将打印condition2模板。如果单击按钮,将调用相应的模板。

app.component.ts

import { Component } from '@angular/core';
@Component({ 
   selector: 'app-root', 
   templateUrl: './app.component.html', 
   styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
   title='Angular 7'; 
   
   //declared array of months. 
   months=["January", "Feburary", "March", "April", "May", "June", "July", 
      "August", "September", "October", "November", "December"];
   
   isavailable=false; //variable is set to true  
   myClickFunction(event) { 
      this.isavailable=!this.isavailable; 
      //variable is toggled onclick of the button 
   } 
   changemonths(event) {
      alert("Changed month from the Dropdown"); 
   }
}

单击按钮,即可切换 isavailable 变量,如下所示-

myClickFunction(event) { 
   this.isavailable=!this.isavailable; 
}

当您基于 isavailable 变量的值单击按钮时,将显示相应的模板-

Condition validInvalid Template

如果您检查浏览器,将会看到您在dom中永远不会获得span标签。以下示例将帮助您理解相同的内容。

Template

尽管在app.component.html中,我们为以下条件添加了span标签和<ng-template>,如下所示:

<span *ngIf="isavailable;then condition1 else condition2">
   Condition is valid.
</span> 
<ng-template #condition1>Condition is valid from template</ng-template>
<ng-template #condition2>Condition is invalid from template</ng-template>

html中的以下代码行将帮助我们在dom中获取span标签-

<!--The content below is only a placeholder and can be replaced.--> 
<div style="text-align:center"> 
   <h1> Welcome to {{title}}. </h1> 
</div>

<div> Months : 
   <select (change)="changemonths($event)" name="month"> 
      <option *ngFor="let i of months">{{i}}</option>
   </select> 
</div> 
<br/>

<div> 
   <span *ngIf="isavailable; else condition2">
      Condition is valid.
   </span>
   <ng-template #condition1>Condition is valid from template </ng-template> 
   <ng-template #condition2>Condition is invalid from template</ng-template> 
</div>
<button (click)="myClickFunction($event)">Click Me</button>

如果删除 then 条件,则在浏览器中将显示"Condition is valid"消息,并且dom中的span标签也可用。如,在 app.component.ts 中,我们将 isavailable 变量设置为true。

Available

这一章《Angular7 - 模板(Templates)》你学到了什么?在下面做个笔记吧!做站不易,你的分享是对我们最大的支持

猜你喜欢

推荐系统三十六式 -〔刑无刀〕

深入浅出gRPC -〔李林锋〕

程序员进阶攻略 -〔胡峰〕

接口测试入门课 -〔陈磊〕

好记忆不如烂笔头。留下你的足迹吧 :)