使用$ scope.$ apply()经常是一个好习惯吗?

ham*_*zox 5 javascript angularjs

更新我的名单时,我是有问题的NG-重复观点和$范围.$适用前来救援.我关注观察者.使用$ scope.$ apply()经常是一个好习惯吗?由于我在应用程序中有很多视图,必须在按钮单击时立即更新.
PS:任何替代方案都表示赞赏.
我的应用程序的示例JS代码:

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");
    })
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

The*_*pti 1

默认情况下,AngularJS 为 JavaScript 异步提供了自己的包装器:

  1. ng-click类似or 的指令ng-keydown
  2. $http异步 AJAX 调用服务
  3. $timeout$interval

$scope.$apply()在我看来,单独使用方法是一种不好的做法,并且不应在整个代码中随机使用该方法。如果必须这样做,则意味着您在考虑应用程序/模块/组件时做错了什么。

在这里阅读更多内容。