I am implementing a sample application and I need to implement a follow button similar to the one in Twitter.
I have coded the button as follows,
<button class="btn pull-right{{setButtonStyle(S.Id)}}"
ng-class="{true:'btn-primary', false:'btn-secondary'}[!S.isFollow]"
ng-click="toggleFollow(S.Id)"> {{!S.isFollow && 'Follow' || 'Unfollow'}}
</button>
The ng-click function handles the DB tables and also toggles the button UI. It is as follows,
$scope.toggleFollow = function (userId) {
var element = $scope.followIds.indexOf(userId);
if (element == -1) {
// Follow user
$scope.Searched[Sindex].isFollow = !$scope.Searched[Sindex].isFollow; // Toggles the button
console.log("Follow called");
})
} else if (element > -1) {
// Unfollow user
$scope.Searched[Sindex].isFollow = !$scope.Searched[Sindex].isFollow; // Toggles the button
console.log("Unfollow called");
})
}
}
The issue is that the button does not toggle at random. I suspect that the AngularJS digest loop doesn't fire every time the button is clicked.
I know for sure that the Angular function gets called every time when the button is clicked. So only the toggle doesn't fire as expected. So how do I force toggle the button every time it is clicked?
Error: [$rootScope:inprog] $apply already in progress