I was having problem when updating my list to ng-repeat in view and $scope.$apply came to the rescue. I am concerned about the watchers. Is it a good practice to use $scope.$apply() frequently? Since I am having many views in application which must be updated immediately on button click.
PS: Any alternatives are appreciated.
Sample JS code of my application:
function onRefreshList() {
vm.showLoader = true;
GetDataService.getVotes(someParams).then(function(res) {
if (res){
vm.showLoader = false;
vm.voteList = res; //voteList will be updated on click of Refresh list
$scope.$apply(); //working fine with this }
}).catch(function (res) {
vm.showLoader = false;
console.log("There was an error will loading votes");
})
}
HTML:
<div ng-show="showLoader">
<ion-spinner icon="android" class="spinner-assertive"></ion-spinner>
</div>
<div ng-show="!showLoader" ng-repeat="vote in votesCtrl.voteList">
{{vote}}
</div>
AngularJS by default provides its own wrappers for JavaScript async:
ng-click
or ng-keydown
$http
service for asynchronous AJAX calls$timeout
and $interval
In my opinion, it is a bad practice to use $scope.$apply()
method on your own and this method shouldn't be used randomly throughout your code. If it has to, it means you did something wrong when you thought about your app/module/component.
Read more here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With