Answer the question
In order to leave comments, you need to log in
Angular 2: Removing page elements based on role?
Good day,
I continue to make a role-dependent system. I practically completed the roles into sections (it remains to figure out where it is better to store the user role). Now I am making roles for separate parts of the section (for example, hide a button, or a list, or ... from a normal user).
I decided to make a directive, an example of the code is below (the role is currently registered statically):
import { Directive, OnInit, ViewContainerRef } from '@angular/core';
import { Input } from "@angular/core/src/metadata/directives";
@Directive({
selector: '[hasRole]'
})
export class HasRoleDirective implements OnInit {
@Input() hasRole: Array<string>;
private viewContainerRef: ViewContainerRef;
constructor(viewContainerRef: ViewContainerRef) {
this.viewContainerRef = viewContainerRef;
}
ngOnInit() {
this.checkRoles('user');
}
checkRoles(userRole: string) {
console.log("Роль пользователя: " + userRole);
if (!this.hasRole || this.hasRole.indexOf(userRole) != -1) {
console.log("Есть доступ");
} else {
this.viewContainerRef.clear();
console.log("Доступ запрещен");
}
}
}
<p [hasRole]="['admin', 'user']">Тестовый параграф которые видят только Админ и Пользователь</p>
<div [hasRole]="['admin']">Тестовый блок который видит только Админ</div>
Answer the question
In order to leave comments, you need to log in
What you missed is that in your case the directive acts as a host for the elements, but you did not add anything to this container called viewContainerRef.
Try writing a structural directive like this:
@Directive({
selector: '[hasRole]'
})
export class HasRoleDirective implements OnInit {
@Input() hasRole: Array<string>;
constructor(private viewContainerRef: ViewContainerRef,
private template: TemplateRef<any>) {}
ngOnInit() {
this.checkRoles('user');
}
checkRoles(userRole: string) {
console.log("Роль пользователя: " + userRole);
if (!this.hasRole || this.hasRole.indexOf(userRole) != -1) {
console.log("Есть доступ");
this.viewContainerRef.createEmbeddedView(this.template);
} else {
this.viewContainerRef.clear();
console.log("Доступ запрещен");
}
}
}
<p *hasRole="['admin', 'user']"><Блок идят только Админ и Пользователь</p>
<div *hasRole="['admin']">Тестовый блок который видит только Админ</div>
<template [hasRole]="['admin', 'user']">
<p>Тестовый параграф которые видят только Админ и Пользователь</p>
</template>
<template [hasRole]="['admin']">
<div>Тестовый блок который видит только Админ</div>
</template>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question