Created
July 2, 2017 07:17
-
-
Save karthikchintala1/3ab6a22c24defc8175195fa40c98d7b9 to your computer and use it in GitHub Desktop.
print only even numbers from structural directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//structural directive demo: To print only even numbers | |
import { Directive, TemplateRef, Input, ViewContainerRef } from '@angular/core' | |
@Directive({ | |
selector: '[onlyEvens]' | |
}) | |
export class EvenDirective { | |
constructor( | |
private templateRef: TemplateRef<any>, | |
private viewContainerRef: ViewContainerRef | |
) { } | |
@Input() set onlyEvens(num: number) { | |
//number is even | |
if (num % 2 == 0) { | |
this.viewContainerRef.createEmbeddedView(this.templateRef); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment