WEB DEVELOPER SITE
PYTHONJAVAPHP7
 

Angular 4 Template


Angular 4 change <template> to <ng-template>.Due to the name conflict between the <template> tag and the html <template> standard tag.Angular 4 is different .

You can use template along with the if else condition and see the output.

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> days :
  <select (change) = "changedays($event)" name = "days">
     <option *ngFor = "let i of days">{{i}}
   </select>
</div>
<br/>

<div>
  <span *ngIf = "isavailable;then condition1 else condition2">Condition is valid.
   <ng-template #condition1>Condition is valid from template
  <ng-template #condition2>Condition is invalid from template
</div>
<button (click) = "myClickFunction($event)">Click Me
The templates are to be called as follows −

<ng-template #condition1>Condition is valid from template</ng-template>
<ng-template #condition2>Condition is invalid from template</ng-template>
If the condition is true, then the condition1 template is called, otherwise condition2.

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

@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'Angular 4 Project!';
   //array of days.
  days = [""Monday ", "Tuesday ", "Wednesday ", "Thursday", "Friday ", 
            "Saturday ", "Sunday "];
   isavailable = false;
   myClickFunction(event) {
      this.isavailable = false;
   }
   changemonths(event) {
      alert("Changed days from the Dropdown");
      console.log(event);
   }
}