3

Hi angular community !

I've got an img who's also a button an I would like to change the button/img as following "each time it's clicked show img "arrow_up" , maybe ngClass changing the css?

My button is an arrow down and each time he is clicked I would like it show an arrow up instead like in this website for exemple Angular Blogspot (if you see the blog archive on right, if you click on it the boutton changed..very common thing)

I provide an exemple with [hidden] but maybe there's a elegantest way to do the job:

html:

<img src="./arrow_down_black.png" type="button"
(click)="toggleChild()"/>
<img [hidden]=""showVar" src="./arrow_up_black.png" type="button"(click)="toggleChild()"/>
   <my-child [showMePartially]="showVar"></my-child>

ts:

clickMe() {
   this.showVar = !this.showVar;
}
2
  • you description is not clear. What do you actually want to achieve ? Commented Aug 10, 2017 at 13:18
  • @Faisal hi Faisal since this morning ! My button is an arrow down and each time he is clicked I would like it show an arrow up instead like in this website for exemple angularjs.blogspot.fr/2017/03/angular-400-now-available.html (if you see the blog archive on right, if you click on it the boutton changed..very common thing) Commented Aug 10, 2017 at 13:22

2 Answers 2

5

Use an expression to display the correct image.

<img src="{{ showVar ? './arrow_down_black.png' : './arrow_up_black.png'}}" 
     type="button" (click)="toggleChild()"/>

     <my-child [showMePartially]="showVar"></my-child>

.. and in your component ts file,

toggleChild() {
   this.showVar = !this.showVar;
}
Sign up to request clarification or add additional context in comments.

1 Comment

many thanks it works fine .. do you know some tutorial where i could learn how to use this typescript expression? I understand ? = if and : = or but didn't find explantations about it..
2

I think binding your img css class to the [ngClass] directive will do the job more elegant way.

<div [ngClass]="{'active': isActive, 'disabled': isDisabled}">

NgClass directive example

3 Comments

according to my code in the post, how could you make...I didn't get it..
@EmileCantero it will look something like that <img [ngClass]="{'name of css class arrrow up img': showVar, 'name of css class arrrow down img': !showVar}" type="button"(click)="toggleChild()"/>
Thanks guys both solutions are working solutions I need to learn more to do disturb you to much

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.