Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
Angular 2 - how to get tag attributes in ngIf function

given such a template

<a *ngIf="canAccess()" routerLink="/adminUsers">...</a>
<a *ngIf="canAccess()" routerLink="/link2">...</a>
<a *ngIf="canAccess()" routerLink="/otherlink">...</a>
<a *ngIf="canAccess()" routerLink="/somthingelse">...</a>

and a function:

canAccess()
{
    ...

how do I get

  • the router link
  • any other tag attribute inside that function

thanks

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Update your template

   <a *ngIf="canAccess($event)" routerLink="/adminUsers">...</a>

and your component

 canAccess(event){

    console.log(event.currentTarget);
    console.log(event.currentTarget.getAttribute("routerlink"));
    ..
    }
over 4 years ago · Santiago Trujillo Denunciar

0

You might need:

  • Location to get the current URL.
  • UrlSegment if you want to check URL parameters.

Put the ones you need into the constructor and call their methods in your canAccess().

In addition you could protect your route (from routerLink) itself with a Guard.

over 4 years ago · Santiago Trujillo Denunciar

0

My solution was to create a directive and it's associated component, so I just give it the module the user wants to access

that directive asks a service if user is logged and can access that link

if yes, the link is remplaced with the component

here is an exemple on how to insert component dynamicaly (not containing the authorization part)

import { Directive, Input, ComponentFactoryResolver,ViewContainerRef } from '@angular/core';
import { MenuItemComponent } from './menuItem.component';

@Directive({ selector: '[menuItem]' })
export class MenuItemDirective {

    constructor(
        private componentFactoryResolver: ComponentFactoryResolver,
        private viewContainer: ViewContainerRef) {}

    @Input() set menuItem(_link: string) {
        if(some sort of condition depending on a auth service)
        {
            const factory = this.componentFactoryResolver.resolveComponentFactory(MenuItemComponent);
            const menuItemComponentRef = this.viewContainer.createComponent(factory);
            menuItemComponentRef.changeDetectorRef.detectChanges();
        }
        else
            this.viewContainer.clear();
  }
}

might be overkill, but in the end I use

<a *menuItem="dashBoard"></a>

instead of

<a class="menuItemA menuRouterLinkA" routerLink="/dashBoard" routerLinkActive="menuCurrentLinkA" >{{ "DashBoard" }}</a>

and it deals with features/page/modules.. access authorisation :-)

yay

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda