====== Directive *ngIf ======
We can use ngIf directive to control an UI element to show or not. The syntax is:
''DOM_ELEMENT'' can be like ''
'', or anything HTML tag. The condition_expression could be a Boolean variable or a function that return Boolean result. ===== Example ===== In this example, we will let user to click a button. And if he clicked it, we will should "The button is clicked" on the top of it. {{ :ng:ng_ngif.png?nolink&400 |}} Assume our component name is ''servers''. ==== servers.component.html ==== Here we user the Boolean variable ''bIsClicked'' to check should the first
...
be shown. A method ''onButtonClicked()'' within the component core code will be called to change the value of ''bIsClicked''.
The button is clicked
==== servers.component.ts ====
Notice we have create a Boolean variable bIsClicked as boolean, and a function onButtonClicked(){...}. On the component init, we set the boolean to false, and if the function onButtonClicked() is called, we set the boolean to true.
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-servers',
templateUrl: './servers.component.html',
styleUrls: ['./servers.component.css']
})
export class ServersComponent implements OnInit {
bIsClicked: boolean;
constructor() { }
ngOnInit() {
this.bIsClicked = false;
}
onButtonClicked() {
this.bIsClicked = true;
}
}