I have a service that has this function
getCurrentUserPermissions() {
return JSON.parse(localStorage.getItem('currentUserPermissions'));
}
Now I create a directive to show and hide menus
*hasPermissions="['stations','users']"
Directive
import {Directive, Input, TemplateRef, ViewContainerRef} from '@angular/core';
import {SiteService} from "../services/site.service";
@Directive({selector: '[hasPermissions]'})
export class HasPermissionsDirective {
@Input("hasPermissions") conditions: string[];
constructor(private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private siteService: SiteService
) {
}
ngOnInit() {
let BreakException = {};
let result: boolean = true;
let currentUserPermissions = this.siteService.getCurrentUserPermissions();
try {
this.conditions.forEach(function (keyCondition) {
if (currentUserPermissions[keyCondition] !== true) {
result = false;
throw BreakException;
}
});
} catch (e) {
if (e !== BreakException) throw e;
}
if (result) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.clear();
}
}
}
How can I watch the this.siteService.getCurrentUserPermissions() for changes? Because now i have to refresh the page to rerender the directive to work, if I change the permissions