ngif angular 4 example
@Directive({ selector: '[ngIf]' }) class NgIf { set ngIf: any set ngIfThen: TemplateRef<NgIfContext> | null set ngIfElse: TemplateRef<NgIfContext> | null }
Showing an alternative template using
Using non-inlined
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 ofngIf
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:
- @Component({
- selector: 'ng-if-simple',
- template: `
- <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button>
- show = {{show}}
- <br>
- <div *ngIf="show">Text to show</div>
- `
- })
- class NgIfSimple {
- show: boolean = true;
- }
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.
- @Component({
- selector: 'ng-if-else',
- template: `
- <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button>
- show = {{show}}
- <br>
- <div *ngIf="show; else elseBlock">Text to show</div>
- <ng-template #elseBlock>Alternate text while primary text is hidden</ng-template>
- `
- })
- class NgIfElse {
- show: boolean = true;
- }
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.
- @Component({
- selector: 'ng-if-then-else',
- template: `
- <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button>
- <button (click)="switchPrimary()">Switch Primary</button>
- show = {{show}}
- <br>
- <div *ngIf="show; then thenBlock; else elseBlock">this is ignored</div>
- <ng-template #primaryBlock>Primary text to show</ng-template>
- <ng-template #secondaryBlock>Secondary text to show</ng-template>
- <ng-template #elseBlock>Alternate text while primary text is hidden</ng-template>
- `
- })
- class NgIfThenElse implements OnInit {
- thenBlock: TemplateRef<any>|null = null;
- show: boolean = true;
-
- @ViewChild('primaryBlock')
- primaryBlock: TemplateRef<any>|null = null;
- @ViewChild('secondaryBlock')
- secondaryBlock: TemplateRef<any>|null = null;
-
- switchPrimary() {
- this.thenBlock = this.thenBlock === this.primaryBlock ? this.secondaryBlock : this.primaryBlock;
- }
-
- ngOnInit() { this.thenBlock = this.primaryBlock; }
- }
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.
- @Component({
- selector: 'ng-if-then-else',
- template: `
- <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button>
- <button (click)="switchPrimary()">Switch Primary</button>
- show = {{show}}
- <br>
- <div *ngIf="show; then thenBlock; else elseBlock">this is ignored</div>
- <ng-template #primaryBlock>Primary text to show</ng-template>
- <ng-template #secondaryBlock>Secondary text to show</ng-template>
- <ng-template #elseBlock>Alternate text while primary text is hidden</ng-template>
- `
- })
- class NgIfThenElse implements OnInit {
- thenBlock: TemplateRef<any>|null = null;
- show: boolean = true;
-
- @ViewChild('primaryBlock')
- primaryBlock: TemplateRef<any>|null = null;
- @ViewChild('secondaryBlock')
- secondaryBlock: TemplateRef<any>|null = null;
-
- switchPrimary() {
- this.thenBlock = this.thenBlock === this.primaryBlock ? this.secondaryBlock : this.primaryBlock;
- }
-
- ngOnInit() { this.thenBlock = this.primaryBlock; }
- }
There are several inefficiencies in the above example:
- We create multiple subscriptions on
userStream
. One for eachasync
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.
0 comments:
Post a Comment