控制器上的Angular Asynchronous函数未返回正确的值

时间:2017-02-13 18:35:05

标签: javascript angularjs angular-promise

我有这个功能来设置是否启用了视图中的某些字段。当流程通过“else”子句时,我收到错误。我想与“then”范围内的“return”句子有关。如何修改代码以正确返回值?

代码

vm.canBeEdited = function() {
  if (vm.viewing || !vm.myBalance || !vm.config || !vm.config.drawerBalanceAllowed) return false;
  if (vm.myBalance.balancingMethod === 'register')
    return vm.myBalance.ownerId && (vm.myBalance.status === 'none' || vm.myBalance.status === 'rejected');
  else {
    cashManagementService.getCashierId(loggedUser.username).then(function(userId) {
      return vm.myBalance.ownerId &&
            (vm.myBalance.status === 'none' || vm.myBalance.status === 'rejected') &&
            ((userId !== undefined && vm.myBalance.ownerId === userId) || securityService.hasRole('app://cash-management/drawer-balances/create'));
    });
  }
};

错误

Unhandled exception at line 146, column 132 in http://localhost:54203/Scripts/angular.min.js

0x800a139e - JavaScript runtime error: [$rootScope:infdig] http://errors.angularjs.org/1.5.8/$rootScope/infdig?p0=10&p1=%5B%5D

这是在Chrome中运行引用的错误,但我不明白如何将其应用于我的代码:

https://docs.angularjs.org/error/$rootScope/infdig?p0=10&p1=%5B%5D

函数getCashierId(name)

function getCashierId(name) {
        var deferred = $q.defer();
        getCatalogs().then(function(catalogs) {
            var cashiers = catalogs.cashiers;
            angular.forEach(cashiers, function(cashier) {
                if (cashier.name == name)
                    deferred.resolve(cashier.id);
            })
        }, function() {
            deferred.reject();
        });
        return deferred.promise;
    } 
function getCatalogs() {
        if (!catalogsResolver)
            catalogsResolver = $http.get(configuration.baseApiUrl + 'catalogs').then(function (response) {
                var catalogs = {
                    cashiers: response.data.cashiers,
                    configuration: response.data.configuration,
                    currencies: response.data.currencies,
                    baseCurrency: response.data.baseCurrency,
                    registers: response.data.registers,
                    tenders: response.data.tenders,
                    safeTenders: response.data.safeTenders
                };
                catalogs.configuration.activeDate = new Date(catalogs.configuration.activeDate);
                return catalogs;
            }, function (data) {
                catalogsResolver = null;
            });
        return catalogsResolver;
    }

控制器使用示例

vm.calculatorVisible = !vm.config.tenderManualEntry && vm.canBeEdited() 

查看使用示例

<label class="radio-inline" ng-class="{'text-muted' : vm.disableCashierRadioButton}">
    <input type="radio"
           ng-model="vm.myBalance.balancingMethod"
           ng-change="vm.balancingMethodChange()"
           value="cashier"
           ng-disabled="!vm.canBeEdited() || vm.disableCashierRadioButton" />
    {{ 'cashManagement.labels.cashier' | translate }}
</label>

0 个答案:

没有答案