Angular7 - 模板(Templates)

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"); 
   } 
}

浏览器中的输出如下-

链接:https://www.learnfk.comhttps://www.learnfk.com/angular7/angular7-templates.html

来源:LearnFk无涯教程网

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Java核心技术面试精讲 -〔杨晓峰〕

Go语言核心36讲 -〔郝林〕

DevOps实战笔记 -〔石雪峰〕

后端存储实战课 -〔李玥〕

动态规划面试宝典 -〔卢誉声〕

数据分析思维课 -〔郭炜〕

郭东白的架构课 -〔郭东白〕

零基础学Python(2023版) -〔尹会生〕

大型Android系统重构实战 -〔黄俊彬〕

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