Skip to content

Instantly share code, notes, and snippets.

@lacolaco
Created November 3, 2017 00:03
Show Gist options
  • Save lacolaco/1779534fdea3cb9cd46053867e770b92 to your computer and use it in GitHub Desktop.
Save lacolaco/1779534fdea3cb9cd46053867e770b92 to your computer and use it in GitHub Desktop.
@Component({
selector: 'ng-if-then',
template: `
<button (click)="switchPrimary()">Switch Template</button>
<div *ngIf="show; then thenBlock"></div>
<ng-template #primaryBlock>Primary</ng-template>
<ng-template #secondaryBlock>Secondary</ng-template>
`
})
class NgIfThenElse implements OnInit {
thenBlock: TemplateRef<any> = null;
show: boolean = true;
@ViewChild('primaryBlock')
primaryBlock: TemplateRef<any> = null;
@ViewChild('secondaryBlock')
secondaryBlock: TemplateRef<any> = null;
switchPrimary() {
this.thenBlock = this.thenBlock === this.primaryBlock ? this.secondaryBlock : this.primaryBlock;
}
ngOnInit() { this.thenBlock = this.primaryBlock; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment