Skip to content

Instantly share code, notes, and snippets.

@karthikchintala1
Created July 2, 2017 07:17
Show Gist options
  • Save karthikchintala1/3ab6a22c24defc8175195fa40c98d7b9 to your computer and use it in GitHub Desktop.
Save karthikchintala1/3ab6a22c24defc8175195fa40c98d7b9 to your computer and use it in GitHub Desktop.
print only even numbers from structural directive
//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