13

I have the following code:

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

Running my app I see the image displayed correctly. However I want the image to change if the current route is active. So I did:

<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>

This on the other hand results in: enter image description here

What am I doing wrong or is this a bug ?

4 Answers 4

13

You should use Angular 2 bindings differently.

Your <img> tag should be:

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

Notice the square brackets around the src attribute, and the template expression between the quotes.

Some reading about property bindings: https://angular.io/docs/ts/latest/guide/template-syntax.html#!#property-binding

Sign up to request clarification or add additional context in comments.

4 Comments

If the bound value is a string, they are equivalent.
what is the difference with my approach ?
@GünterZöchbauer I wasn't aware but after reading angular.io/docs/ts/latest/guide/… I must agree :)
I like when people agree with me :)
2

I was able to solve it by doing a unary 'IF' in nested form

    <td class="cent">
         <img  [src]="OrdersFund?.status=='P' ? 'assets/P.png' : OrdersFund?.status=='E' ? 'assets/E.png' : 'assets/C.png'" /> 
    </td>

Comments

1

I came across with same problem. My solution is a bit tricky to change src of image.
In html:

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

In angular:

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

Comments

1

I found the problem. This is the correct way.

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

I was using src={{}} instead of src="{{}}".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.