27 lines
1.0 KiB
TypeScript
27 lines
1.0 KiB
TypeScript
import { Directive, Input, OnInit, TemplateRef, ViewContainerRef } from '@angular/core';
|
|
import { Store } from '@ngrx/store';
|
|
import { Observable } from 'rxjs';
|
|
import { IUser } from '../../models/user';
|
|
import * as appCommonReducer from '../../reducers/app-common.reducer';
|
|
|
|
@Directive({
|
|
selector: '[fm-hasclaim]',
|
|
})
|
|
export class HasClaimDirective implements OnInit{
|
|
@Input('fm-hasclaim') claim:string;
|
|
|
|
constructor(private templateRef$: TemplateRef<any>,private viewContainerRef$: ViewContainerRef,private store$: Store<appCommonReducer.State>) { }
|
|
private user$:Observable<IUser> = this.store$.select(appCommonReducer.SelectGetUser);
|
|
private hasView = false;
|
|
ngOnInit() {
|
|
this.user$.subscribe((user) => {
|
|
if (user && user.claims[this.claim]) {
|
|
this.viewContainerRef$.createEmbeddedView(this.templateRef$);
|
|
this.hasView=true;
|
|
} else if (this.hasView) {
|
|
this.viewContainerRef$.clear();
|
|
this.hasView = false;
|
|
}
|
|
});
|
|
}
|
|
} |