angularangular-directiveangular-routerlink

How can access to routerLink value via custom directive?


How can I create a custom directive(e.g. appCheckPermission ) to check if user can view a link or button base on routerLink that applied to that element.

<button [routerLink]="['/users/edit', userId]" appCheckPermission ></button>

p.s: there is a property that save routerlink value

nativeElement.attributes["ng-reflect-router-link"].value

but can not be access via custom directive.


Solution

  • It should be accessible as an Input property and inside the lifecycle hooks

    @Directive({ selector: '[appCheckPermission]' })
    export class AppCheckPermissionDirective implements OnInit {
      @Input() routerLink: any;
    
      ngOnInit(): void {
        console.log(this.routerLink);
      }
    
      ...
    }