2017-10-25 7 views
1

ルータリンク属性をプログラム的に追加するにはどうすればよいですか?内訳角度2 - 構造指令 - ルータリンクを挿入する方法

@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); 
} 

は可能ですか? またはrouterLinkを扱うコンポーネントを注入する必要がありますか?

おかげ

+0

ディレクティブを追加することはできませんから。ディレクティブを動的に追加することはできません – yurzui

+0

例がありますか?私はhow.Tanksを参照してくださいない – phil1234

+1

https://stackblitz.com/edit/angular-etlype?file=app/app.component.html – yurzui

答えて

0

yurzui

1は `NG-template`を使用して動的に

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