I'm currently making a frontend app for a project using angular 4, from the backend I get some actions called with a POST that are the same:
actions.response.ts
export class actions{
AGREEMENTS_VIEW :string;
PROSPECTS_VIEW :string;
AGREEMENTS_INSERT_UPDATE :string;
PRODUCTS_INSERT_UPDATE :string;
PROSPECTS_INSERT_UPDATE :string;
DOCUMENTS_VIEW :string;
DOCUMENTS_INSERT_UPDATE :string;
}
Now, what I want to do:
Based on each actions (agreements_view, prospects_view.. etc) i want to enable or disable a component or some input/select/button... How can I do that?
http post:
securityActions(): Observable<actions> {
return this.http.post<actions>(
`${this.ENDPOINT}/security-actions`,
null,
);
}
How i called the post inside the component:
securityActions() {
this.securityService.securityActions().subscribe(
(res: actions) => {
this.actionsSecurity = res;
console.log(res);
},
errors => {
Utils.notifyErrors(errors, this.notificationsService);
});
}
Sorry if my question sounds stupid but im new to angular and im kinda lost!
In my current project we created a permission directive. You give it some conditions and it deletes the tags from the view when it doesn't match.
Here is a sample of it :
For what i understand you want to activate or deactivate access to a component or button regarding some rules. For example if the user is logged in or not or if your form is properly validated. If you want to deactivate a button you can use this directive here [disabled]:
for example if your form is not valid you cant submit the data.
For components you can do this on routes. You need first to make a service that implements the
CanActivate
interface. And for example for authetication you could do like this:And finally on your routes files just add the rule there. For example to have access to dashboard only if is authenticated.
I hope this example can help you.And let me know if you need anything or its not this your looking for.