Marco Marco - 4 months ago 9x
AngularJS Question

Angular changing ng-class on click

I am trying to change a class that I am creating based on a value that I get from json, and then change it if user cliks on it, but I am not getting that to work.

This is my code in the controller:

$ = function(){
if ($scope.class === "ion-ios-heart-outline")
$scope.class = "ion-heart";
$scope.class = "ion-ios-heart-outline";

And the element in the view:

<i ng-click="like()" ng-class="{ 'ion-heart' : == 1, 'ion-ios-heart-outline' : == 0}">

str str

You are mixing $scope.class (which is never used in the view) and ng-class, so I am not entirely sure what you want to do. But I guess what you are looking for is this:

$ = 0;
$ = function() {
    $ = $ == 0 ? 1 : 0;

Then the CSS classes will be changed based on whether the article was liked or not.