2016-11-23 5 views
1

私は、次のコードを持っている:Angular2ダイナミックのIMG SRC

<a routerLink="/dashboard" routerLinkActive="active" #rla="routerLinkActive"> 
      <img src="/assets/navigation/dashboard-icon-active.svg" /> 
     <template [ngIf]="!isSmallSidebar"> 
      Dashboard 
     </template> 
     </a> 

が私のアプリを実行すると、私は画像が正しく表示され参照してください。しかし、現在のルートがアクティブであれば、イメージを変更したい。だから私はやった:における一方の結果に

<a routerLink="/dashboard" routerLinkActive="active" #rla="routerLinkActive"> 
     <!-- <img 
      id="re-nav-dashboard-img" 
      src={{ rla.isActive ? './assets/navigation/dashboard-icon-active.svg' : './assets/navigation/dashboard-icon.svg' }} /> --> 
     <template [ngIf]="!isSmallSidebar"> 
      Dashboard 
     </template> 
     </a> 

この: enter image description here

は私が間違って何をやっているか、これはバグでしょうか?

答えて

0

問題が見つかりました。これは正しい方法です。

<img 
      id="re-nav-dashboard-img" 
      src="{{rla.isActive ? './assets/navigation/dashboard-icon-active.svg' : './assets/navigation/dashboard-icon.svg'}}" /> 

Iは、Src = {{}}代わりSRCの= " {{}} "を用いました。

2

角度2のバインディングを別に使用する必要があります。

あなた<img>タグは次のようになります。

<img id="re-nav-dashboard-img" [src]="rla.isActive ? './assets/navigation/dashboard-icon-active.svg' : './assets/navigation/dashboard-icon.svg'" />

お知らせsrc属性の周りの角括弧、および引用符の間にテンプレート式。プロパティのバインディングに関する

いくつかの読書:https://angular.io/docs/ts/latest/guide/template-syntax.html#!#property-binding

+1

界値が文字列である場合、それらは同等です。 –

+0

私のアプローチとの違いは何ですか? – mp3por

+1

@GünterZöchbauer私は気づいていませんでしたが、https://angular.io/docs/ts/latest/guide/template-syntax.html#interpolationを読んだ後、私は同意しなければなりません:) –

0

は、私は同じ問題に出くわしました。私の解決策はイメージのsrcを変更するのはややこしいことです。 HTMLで
:角度で

<div id="imageDiv"></div> 

document.getElementById('imageDiv').innerHTML = '<img src="' + YourPhotoURLOrBase64String + '"/>';