TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Angular 7 ngIf Directive

Angular 7 ngIf Directive with angular7, tutorial, introduction, angular, js, Installation, History and Versions, First App, files explanation, Components, Directives, ngIf Directive, ngFor Directive, ngClassDirective, ngStyle Directive, Angular7 with Bootstrap etc.

<< Back to ANGULAR

Use of *ngIf directive to change the output conditionally

Example:

component.ts file:

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

@Component({
  selector: 'app-server2',
  templateUrl: './server2.component.html',
  styleUrls: ['./server2.component.css']
})
export class Server2Component implements OnInit {
 allowNewServer = false;
 serverCreationStatus = 'No server is created.';
  serverName = 'TestServer';
  serverCreated = false;

  /*constructor() {
    setTimeout(() =>{
      this.allowNewServer = true;
    }, 5000);
  }*/

  ngOnInit() {
  }
  onCreateServer() {
    this.serverCreated = true;
    this.serverCreationStatus = 'Server is created. Name of the server is' + this.serverName;
  }
  OnUpdateServerName(event: Event) {
    this.serverName = (<HTMLInputElement>event.target).value;
  }
}

component.html file:

<p>
  Server2 is also working fine.
</p>

<label>Server Name</label>
<!--<input type="text"
       class="form-control"
       (input)="OnUpdateServerName($event)">-->
<input type="text"
       class="form-control"
[(ngModel)]="serverName">
<!--<p>{{serverName}}</p>-->
<button
  class="btn btn-primary"
  [disabled]="allowNewServer"
  (click)="onCreateServer()">Add Server</button>
<p *ngIf="serverCreated"> Server is created. Server name is {{serverName}}</p>

Output:

The output will look like this.

Use of *ngIf directive to change the output conditionally

When we change the input value and click on "Add Server" button, you will see the following result:

Use of *ngIf directive to change the output conditionally

You can see in the above example that by using *ngIf directive, we can change the condition to display the output accordingly.

You can check the view source of your output before and after the adding the server. You will see the difference clearly.

Before adding server:

Use of *ngIf directive to change the output conditionally

After adding the server:

Use of *ngIf directive to change the output conditionally

So, you can see that how a structural directive can change the DOM.

*ngIf directive with an Else condition

You can also use an Else condition with *ngIf directive. It is used to display the output if *ngIf is not true. Let's make some changes in component.html file.

component.html file:

<p *ngIf="serverCreated; else noServer"> Server is created. Server name is {{serverName}}</p>
<ng-template #noServer>
  <p>No Server is created.</p>
</ng-template>

Output:

Use of *ngIf directive to change the output conditionally

After clicking on "Add Server" button:

Use of *ngIf directive to change the output conditionally

You can also check its reverse case by using the negation (!) sign.

<p *ngIf="!serverCreated; else noServer"> Server is created. Server name is {{serverName}}</p>
<ng-template #noServer>
  <p>No Server is created.</p>
</ng-template>





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf