$scope.apply 正在发射 "catch" 以及 "then"

$scope.apply is firing "catch" as well as "then"

在 Angular 1.6.2 和 UI 路由器中,我希望仅在服务器上建立用户具有正确的页面内容后显示页面内容 role/permissions 访问页面。

没有 $scope.apply() catch() 不会被解雇,但出于某种原因,当我把它放在那里时情况并非如此。如果没有 $scope.apply()vm.showContent 变量似乎不会更新视图。

我没有收到 Angular 或 JS 错误,所以我说我会省略任何其他相关代码,因为我认为没有其他原因导致问题。

View/HTML

<div ng-show="data.showContent">
 // my html, not to be shown until vm.showContent is true     
</div>

控制器

// more JS
var vm = this;
vm.showContent = false;
// more JS
vm.hasRole = function (role, toState, event) {

    Auth.hasRole(role).then(function (data) {
        vm.showContent = true;
        // without this nothing is happening with the view
        $scope.apply();
        alert('has role'); // firing successfully
    }).catch(function () {
        alert('does not have role') // firing also if I add $scope.apply();
        if (event != false) {
            event.preventDefault();
            $state.go('no-permission');
        }
    });
}

查看错误响应值:

//}).catch(function () {
//LOG error response
}).catch(function(errorResponse) {
    console.warn(errorResponse);
    alert('does not have role') // firing also if I add $scope.apply();
    if (event != false) {
        event.preventDefault();
        $state.go('no-permission');
    }
});

$scope.apply() 可能抛出:

Error: $rootScope:inprog Action Already In Progress

当在承诺链中的成功处理程序中抛出错误时,$q 服务会跳到链中的第一个后续拒绝处理程序。

有关该错误的详细信息,请参阅 AngularJS Error Reference - $rootScope/inprog


AngularJS 1.6 将抛出的错误与常规拒绝处理相同

对于以前的版本,成功和拒绝处理程序中抛出的错误会创建控制台错误消息。 AngularJS 1.6 已更改为将抛出的错误视为与常规拒绝相同:

$q:

Due to e13eea, an error thrown from a promise's onFulfilled or onRejection handlers is treated exactly the same as a regular rejection. Previously, it would also be passed to the $exceptionHandler() (in addition to rejecting the promise with the error as reason).

--AngularJS Developer Guide - Migrating from v1.5 to v1.6 - $q