ngif angular 4 example - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
ngif angular 4 example

ngif angular 4 example

ngif angular 4 example

 ngIf evaluates the expression and then renders the then or else template in its place when the expression is truthy or falsy respectively

@Directive({ selector: '[ngIf]' }) class NgIf { set ngIf: any set ngIfThen: TemplateRef<NgIfContext> | null set ngIfElse: TemplateRef<NgIfContext> | null }

ngIf evaluates the expression and then renders the then or else template in its place when expression is truthy or falsy respectively. Typically the:

  • then template is the inline template of ngIf unless bound to a different value.
  • else template is blank unless it is bound.

Most common usage

The most common usage of the ngIf directive is to conditionally show the inline template as seen in this example:
  1. @Component({
  2. selector: 'ng-if-simple',
  3. template: `
  4. <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button>
  5. show = {{show}}
  6. <br>
  7. <div *ngIf="show">Text to show</div>
  8. `
  9. })
  10. class NgIfSimple {
  11. show: boolean = true;
  12. }

Showing an alternative template using else

If it is necessary to display a template when the expression is falsy use the else template binding as shown. Note that the else binding points to a <ng-template> labeled #elseBlock. The template can be defined anywhere in the component view but is typically placed right after ngIf for readability.
  1. @Component({
  2. selector: 'ng-if-else',
  3. template: `
  4. <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button>
  5. show = {{show}}
  6. <br>
  7. <div *ngIf="show; else elseBlock">Text to show</div>
  8. <ng-template #elseBlock>Alternate text while primary text is hidden</ng-template>
  9. `
  10. })
  11. class NgIfElse {
  12. show: boolean = true;
  13. }

Using non-inlined then template

Usually the then template is the inlined template of the ngIf, but it can be changed using a binding (just like else). Because then and else are bindings, the template references can change at runtime as shown in this example.
  1. @Component({
  2. selector: 'ng-if-then-else',
  3. template: `
  4. <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button>
  5. <button (click)="switchPrimary()">Switch Primary</button>
  6. show = {{show}}
  7. <br>
  8. <div *ngIf="show; then thenBlock; else elseBlock">this is ignored</div>
  9. <ng-template #primaryBlock>Primary text to show</ng-template>
  10. <ng-template #secondaryBlock>Secondary text to show</ng-template>
  11. <ng-template #elseBlock>Alternate text while primary text is hidden</ng-template>
  12. `
  13. })
  14. class NgIfThenElse implements OnInit {
  15. thenBlock: TemplateRef<any>|null = null;
  16. show: boolean = true;
  17.  
  18. @ViewChild('primaryBlock')
  19. primaryBlock: TemplateRef<any>|null = null;
  20. @ViewChild('secondaryBlock')
  21. secondaryBlock: TemplateRef<any>|null = null;
  22.  
  23. switchPrimary() {
  24. this.thenBlock = this.thenBlock === this.primaryBlock ? this.secondaryBlock : this.primaryBlock;
  25. }
  26.  
  27. ngOnInit() { this.thenBlock = this.primaryBlock; }
  28. }

There are several inefficiencies in the above example:
  • We create multiple subscriptions on userStream. One for each async pipe, or two in the example above.
  • We cannot display an alternative screen while waiting for the data to arrive asynchronously.
  • We have to use the safe-traversal-operator ?. to access properties, which is cumbersome.
  • We have to place the async pipe in parenthesis.

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.