The HTML will remain the same
<a href="" ng-click="addFavorite(myfav.id);favorite=!favorite">
<i ng-class="{'fa-star-o':!favorite,'fa-star':favorite||fav===myfav.id}"></i>
</a>
But the order in which classes are present in your CSS file will matter
The fa-star
class will apply either when favorite
is true or fav===myfav.id
returns true.
Therefore if after clicking once , suppose fav===myfav.id
returns true and keeps on returning true , even when clicking again , then the class fa-star
will be applied always from then on.
If by default favorite
is false , then fa-star-o
will be applied when template is loaded the first time, but after the first click ,when favorite
is set to true , it will get removed. Then on second click , when favorite
is set to false again , fa-star-o
it will get applied but in this case , fa-star
class will also be applied as fa===myfav.id
condition would be still returning true (Assuming that is the case).
Therefore you will need to prioritize which class needs to get applied for sure when it is present on the element as case can arise when both classes can be present at the same time on the element. For example if fa-star-o
class takes higher priority, then put it below the fa-star
in your CSS , like for example
.fa-star {
border: 1px solid #F00;
}
.fa-star-o {
border: 1px solid #000;
}
See working demo at http://plnkr.co/edit/Dh59KUU41uWpIkHIaYrO?p=preview
{'fa-star': favorite || fav == myfav.id, 'fa-star-o': !favorite}
. See a similar example in this plunkr. I tried to replace that code withng-class="{'has-error': ctrl.submitted && 1 == 1}
and it works. – Cliffcliffesfav == myfav.id
outside and it returns "true" as it should be. – Finkelstein!favorite
butfav==myfav.id
the result would be bothfa-star
fa-star-o
and then the last one applies and visually doesn't change the icon. So I'm not sure my "switch" works in this case – Finkelstein