+ - 0:00:00
Notes for current slide
Notes for next slide

Angular Structural Directives

By: Vahid Mohammadi

Sample code is available in angular-sample/src/app

Created with: RemarkJS
1 / 4

*ngFor

Get index, first, last, even, odd of current element

When are changes propagated to the DOM?

  • When an element is added to the array
  • When an element is removed from the array
  • When items are reordered

Optimizing *ngFor

By default ngFor is optimized by array elements reference, But sometimes we have immutable data.

Use trackBy

Use objects in *ngFor

You can use keyvalue pipe

2 / 4

*ngIf

  • Try not to call functions!
    • Performance
    • It may not detect changes
  • else and then else
  • Create scoped variables with *ngIf
  • *ngIf vs [hidden]: *ngIf is for null check but [hidden] is not. You can also use ?. and !. in Angular template files
3 / 4

ng-template and ng-container

  • use * directives without creating an element (ex. <tr>)
  • use multiple * directives
  • display ng-templates
  • dispaly dynamic components

Being “virtual” means the contents won’t actually exist in the compiled DOM

ng-content

  • selector can be tag, [attribute] or .css-class

View & Content Children

  • @ViewChild() and @ViewChildren()
  • @ContentChild() and @ContentChildren()

Directives exportAs

You can export directives to a #template

4 / 4

*ngFor

Get index, first, last, even, odd of current element

When are changes propagated to the DOM?

  • When an element is added to the array
  • When an element is removed from the array
  • When items are reordered

Optimizing *ngFor

By default ngFor is optimized by array elements reference, But sometimes we have immutable data.

Use trackBy

Use objects in *ngFor

You can use keyvalue pipe

2 / 4
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow