billyJoe
billyJoe

Reputation: 2064

Add an event on ng-show

i'm not an angular master and i try to do something. Suppose a fonctionality like "i like / i dislike". When you're on an article, you can click on the "i like" button. If you already liked the article, this button is hidden and the "i don't like anymore" appears.

    <button ng-hide="like" class="btn btn-success btn-xs pull-right" ng-click="iLike()">I like</button>
<button ng-show="like" class="btn btn-danger btn-xs pull-right" ng-click="notLike()">Don't like anymore</button> 

Everything work as expected when i reload the page but not on click action. Basically my iLike function looks like and i think miss behavior comes from a missing return or an event to fire :(

$scope.iLike = function(){
        ##hereIDoAnAPICalls##, function(){ 
           $scope.like = false;
        }, function(){
            $scope.like = true;
        }
    }

Upvotes: 0

Views: 71

Answers (1)

doodeec
doodeec

Reputation: 2927

Try to wrap scope variable assignment into apply, I think it could help since API call is probably asynchronous and click event is invoked from outside of angular

$scope.iLike = function() {
    APIcall.then(function () {
        $scope.$apply(function() {
            $scope.like = false;
        });
    }, function () {...the same...});
};

Upvotes: 1

Related Questions