I'm trying to create a directive that works as a ngIf to control if the user with the correct permission is allow to see specific content, something like this:
<div [type-of-user]="load data from the user and check it (normal or premium)">
<h3>You are allow to see this.</h3>
</div>
I was reading about how to do it and found on this doc about "Attribute Directives" but I still can't implement it (I am new with Angular 2)
So far I have this:
import {Directive, ElementRef, Input} from '@angular/core';
@Directive({
selector: '[type-of-user]'
})
export class TypeOfUserDirective{
constructor(private el: ElementRef){}
@Input('type-of-user') userControl: string;
private haspermission(permission: string) {
/*the style background color is just to test if works*/
this.el.nativeElement.style.backgroundColor = permission;
}
}
I already added the directive in the app.module.
Any advice how to proceed would be great.
if
condition you are trying to re invent the wheel .. check *ngIf – Scandal