1

I found a very neat solution on another stackoverflow post by Thierry Templier, however it is out dated for Angular 2 RC5 new router.

Basically this is the secure directive:

@Directive({
  selector: '[secured]'
})
export class SecuredDirective implements OnInit {
  @HostBinding('hidden') hideRouterLink: boolean = false;

  @Input() routerLink: string;

  constructor(private router: Router ) {
    console.log(router);
  }

  ngOnInit(): void {
    console.log(this.routerLink);

    // How to get routeData here??
    // Auth logic
    this.hideRouterLink = true;

  }
}

Previously on angular's old router we could do this:

var instruction = this.router.generate(this.routeParams);
var data = instruction.component.routeData.data;

Now with RC5, I can't figure out a way how to get the router instruction. Is there another way to do this?

Edit: more info

What I am trying to achieve is to grab the route data defined in the routing.ts example:

{ path: 'control-panel', component: ControlPanelComponent, data: { 'resources': 'controlpanel', 'privileges': 'admin'} }

Any help is greatly appreciated, thanks!

Community
  • 1
  • 1
penleychan
  • 5,370
  • 1
  • 17
  • 28

1 Answers1

0

You can try below,

@Directive({
 selector: '[secured]'
})
export class SecuredDirective implements OnInit, AfterViewInit  {
  _hideRouterLink: string = '';
  @HostBinding('style.display') get hideRouterLink() {
    return this._hideRouterLink; 
  } 
  set hideRouterLink(val: boolean){
    this._hideRouterLink =  val;
  }

  @Input('routerLink') routeParams:  any[];

  constructor(private router: Router ) {
   console.log(router);
  }

  ngAfterViewInit(){  
  }

  ngOnInit(): void {
    console.log(this.routeParams);
 
    // Auth Logic
    this.hideRouterLink = 'none';
  }
}

Here is the Plunker!

Madhu Ranjan
  • 17,334
  • 7
  • 60
  • 69
  • How do I retrieve the RouteData though? Your solution is similar to mine. I need a way to retrieve the RouteData defined in the routes, I updated my question for more info. Thanks – penleychan Aug 20 '16 at 19:42
  • @12seconds, you may get data from this.router.config, issue is if the route is lazily loaded you wont find that, I am also looking for some solution for this, If I find something will update the answer. – Madhu Ranjan Aug 22 '16 at 02:11