Javascript 经常使用$scope.$apply()是否是一种好的做法?

Javascript 经常使用$scope.$apply()是否是一种好的做法?,javascript,angularjs,Javascript,Angularjs,在视图中将列表更新为ng repeat和$scope时,我遇到了问题。$apply起了作用。我担心观察者。经常使用$scope.$apply()是否是一种好的做法?因为我在应用程序中有许多视图,必须在单击按钮时立即更新这些视图。 PS:欢迎您提供任何选择。 我的应用程序的示例JS代码: function onRefreshList() { vm.showLoader = true; GetDataService.getVotes(someParams).then(function

视图中将列表更新为ng repeat和$scope时,我遇到了问题。$apply起了作用。我担心观察者。经常使用$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");
    })
}
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默认为JavaScript异步提供自己的包装器:

  • 指令,如
    ng click
    ng keydown
  • $http
    异步AJAX调用服务
  • $timeout
    $interval
  • 在我看来,单独使用
    $scope.$apply()
    方法是一种不好的做法,并且这种方法不应该在整个代码中随机使用。如果必须这样做,这意味着你在考虑应用程序/模块/组件时犯了错误


    阅读更多信息。

    在你喜欢的搜索引擎上,数百条“何时使用…?”、“最佳实践使用…”、“我应该何时使用…?”的搜索结果都没有帮助?伙计,我比任何人都更尊重stackoverflow。你真的认为我没试过吗?没有得到我想要的结果。应该只在更新范围的代码超出角度上下文时才需要使用它。假设
    GetDataService.getVoces()
    使用了第三方javascript api,您可能希望转而考虑返回
    $q
    承诺。他们被设置为不需要
    $apply()
    ,因为他们称之为内部可能重复的感谢大家的帮助。谢谢兄弟。