0

I want that when I click this button, the 'glyphicon-plus' change to 'glyphicon-minus' and when i again click, it will again 'glyphicon-plus'. I want It should be Toggelable. I dont want it in css or jquery. app.component.html :

<button data-toggle="collapse" 
        [attr.data-target]="'#demo' + RowIndex">
        <span class="glyphicon glyphicon-plus"></span>
</button>
Er Vipin Sharma
  • 2,519
  • 8
  • 22
  • 32

1 Answers1

3

In your template, use a sign binding and a click event binding as follows:

<button data-toggle="collapse" 
        (click)="toggleSign()"
        [attr.data-target]="'#demo' + RowIndex">
        <span class="glyphicon glyphicon-{{sign}}"></span>
</button>

In your component class, change the sign property on each click.

export class MyComponent {
   ....
   sign = 'plus';  // or minus if you want that first

   toggleSign() {
     if(this.sign == 'plus') {
       sign = 'minus';
     } else {
       sign = 'plus';
     }
   }
  ......

 }

Always think of things this way in Angular - my view as a reflection of the state of my components. So if i want to change things visually, using the binding system to bind the variable parts of my view to properties of my components and just change the component properties to reflect the view that i want.

snorkpete
  • 14,278
  • 3
  • 40
  • 57
  • Great snorkpete... This is working for me. Thanks :) – Er Vipin Sharma Apr 13 '17 at 10:39
  • I have another question. Can you please look into that. I am giving link here for that: http://stackoverflow.com/questions/43391568/how-to-add-a-tooltip-on-inputbox-dynamicaly-in-angular-2. Thanks – Er Vipin Sharma Apr 13 '17 at 11:53
  • @snorkpete It is good solution. But when I fast clicking, the icon was change too fast - not waiting for open/close collapse field. Do You have maybe idea how to prevent for that? – profiler Mar 16 '18 at 08:38
  • @snorpete what if there are multiple buttons – nas Jan 28 '19 at 15:18