Search code examples
angularangular2-directives

Angular 2 - Structural Directive - How to insert a router link


How do I add a routerLink attribute programmaticaly ? inside a directive

@Input() set siteMapLeaf(caption:string)
{
    let view = this.templateRef.createEmbeddedView({});
    const parentNode = view.rootNodes[0];

    // <a routerLink="dashBoard">Some Text</a>
    const leaf = this.renderer.createElement('a');
    const text = this.renderer.createText(caption);
    this.renderer.appendChild(leaf, text);
    this.renderer.addClass(leaf,'siteMapLeafDiv');
    this.renderer.setAttribute(leaf,'routerLink','dashBoard');

    this.renderer.insertBefore(parentNode,leaf, parentNode.firstChild);
    this.viewContainer.insert(view);
}

is it possible ? or do I have to inject a component that deals with the routerLink ?

thanks


Solution

  • from yurzui

    one can't add directive dynamically

    <hello name="{{ name }}"></hello>
    <div *siteMapLeaf="'Campaign' as item">
      <a class="siteMapLeafDiv" [routerLink]="item.link">{{item.text}}</a>
    </div>