§ Directives help us is to enable us to give instructions to the Angular on how to render the templates to the DOM. So, they use directives to give instructions to Angular as it is rendering the templates on the screen.
§ Angular templates are dynamic. When Angular renders them, it transforms the DOM according to the instructions given by directives. A directive is a class with a @Directive() decorator.
§ There are two other kinds of directives in Angular, structural and attribute directives.
Structural Directives§ There are three common built in structural directives namely
· ngIf
· ngSwitch
ngIf
§ Structural directives ngIf and ngSwitch which are used to conditionally render HTML elements
§ Since ngIf is a structural directive it is prefixed with *.
§ When you use ngIf in your template syntax you will write it as *ngIf
§ For example, if you apply the *ngIf directive to a div like this in this example, what you're specifying where is that if the selectedItem is not null then this div will be added to the DOM. If that selectedItem is null, then this div, whatever is contained in this div will not be added to the DOM, so you are literally removing this from the DOM if the value turns out to be false. If it turns out to be true, then it is added into the DOM.
app.component.html
Here we are using angular material toolbar.
When we are working with angular material toolbar we need to import MatToolbarModule in app.module.ts file.
Note:-
§ If angular material library is not added in your project then first add angular material library in your project by following commands.
app.component.html
§ Save the project and run the project by the following command in
terminal.
Output
§ let's back in our code I change true to false
save this and take a look at the browser gravity4Tech.blogspot.com is not
visible anymore.
Output
§ Setting true or false on the right-hand side is of not much let's assign a property value now we create a new property call isDisplay name and set it true and assign it to the directive.
app.component.ts
app.component.html
§ Save the project and check the output
*ngIf and Else: if statement with else
§ When we are working with *ngIf directive can provide an expression which return boolean flag true of false to add or remove the element form DOM having *ngIf directive.if *nfIf directives returns false then we want to perfrom another task then you can use else also with *ngIf.
§ *ngIf have the else statement with if to handle conditions
§ The <ng-template> is an Angular element for rendering HTML.
§ let's take a look an example to see how to
implement and else block for the ngIf directive.
let's take a look at another Syntax that angular provide for the ng-if directive.
§ In Angular 4.x.x You can use ngIf in four way to
achieve simple if else procedure:
Just Use If
Using If with Else (Please notice to templateName)
Using If with Then (Please notice to templateName)
Using If with Then and Else
Note:-
§ ngIf evaluates the expression and then renders the then or else template in its place when expression is truthy or falsy respectively
1) *ngIf
2) *ngIf and Else
3) *ngIf, Then and Else
§ You can Use <ng-container> and <ng-template> for
Achieve This
Just add new updates from Angular 8.
1. For case if with else, we can use ngIf and ngIfElse.
2. For case if with then, we can use ngIf and ngIfThen.
3. For case if with then and else, we can use ngIf, ngIfThen,
and ngIfElse.