1

I have this images:

<div class="image-index-container">
                    <div>
                        <img class='index-pics' src="images/yourbag.png" alt="">
                        <p class="activeIndex">Your Bag</p>
                    </div>
                    <hr>
                    <div style="width: 13%">
                        <img class='index-pics' src="images/delivery-gray.png" alt="">
                        <p>Delivery Details</p>
                    </div>
</div>

when I click on one button I want to change the src attribute from images/yourbag to images/yourbag-gray and the img with the src images/delivery-gray to images/delivery.

in addition to remove the class activeIndex from the first text and add it to the second one without using JQuery

2 Answers 2

2

do it like this using this [src]="imageUrl"

import {Component} from '@angular/core'

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
    <img class='index-pics' [src]="imgsrc" alt="">
    </div>
    <button type="button" (click)="btnClick()">change</button>
  `,
  directives: []
})
export class App {
  imgsrc="http://az866755.vo.msecnd.net/product/1468402190267-product.jpeg"

  btnClick()
  {
   this.imgsrc="http://az866755.vo.msecnd.net/product/1454067764999-product.jpeg" 
  }

  constructor() { }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if my image has an imgsrc and srcset like: srcset="images/[email protected] 2x, images/[email protected] 3x"? same approach?
use [srcset] like this
1

Use src="{{path}}" and Angular will update the path. For example

<button (onclick)="path=newpath"><button>

Another example

<div class="col-lg-4">
<h3>Github Users:{{[].concat(users).length}} </h3>
<ul>
  <li *ngFor="let user of users" style="list-style:none">
   <button (click)="data = user" style="cursor:pointer"> <img  src="{{user.avatar_url}}" style="height:200px; width:250px;" /></button>
  </li>
</ul>
</div>
<md-card class="col-lg-8 " *ngIf="data" style="position:fixed; margin-left:22%; margin-top:3.5%">
  <div class="col-lg-4">
  <img  src="{{data.avatar_url}}" style="height:200px; width:250px;" />
  </div>
  <div class="col-lg-8">
{{data | json}}
 </div>
</md-card>

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.