从angular.js中一个接一个地从子控制器调用两个父控制器作用域函数

问题描述 投票:0回答:1

我有一个来自子控制器的Angular.js方法,它一个接一个地调用两个父控制器方法。但第二个函数应该从第一个函数获取帐户数据,然后将更新如下所示的调用数据:

filter.filterAccountsByProductMetrics1 = function(productWithSegmentations12) {
    accountService.fetchAccountForRecordType([filter.selectedHcpHco.Name.display])
    .then(function(resp) {  
        $scope.accountDataUpdate({
            accounts: resp.data
        });
        var productId = null;
        if(filter.selectedMySetupProduct.Product_vod__c) {
            productId = filter.selectedMySetupProduct.Product_vod__c.value;
        }
        callService.getCallsForProductId(productId)
        .then(function(calls) {
            filter.filterRecords[filterType.product.value] = calls;
            $scope.callDataUpdate({
                calls: applyAllFilterOnCalls()
            });
        });
    });
};  

我检查了两个函数都被调用但序列没有被维护。如何确保两个父函数一个接一个地调用。

编辑:function accountDataUpdate:

call.accountDataUpdate = function(accounts) {
    call.accounts = accounts;
    getCallDetails(getCallIdsFromCallsForFilteredAccount())
    .then(function() {
        updateProductFrequencyTableData();
        updateAccountDetailData(true);
    });

    updateDailyFrequencyChartData();
    updateWeeklyFrequencyChartData();
    updateCallFrequencyTableData();
    updateAccountFrequencyData();

    $timeout(function() {
        $scope.$broadcast('updateDoughnutChart');
        $scope.$broadcast('updateBarChart');
    });
};
angularjs methods controller angularjs-scope parent-child
1个回答
1
投票

修改accountDataUpdate以返回承诺:

call.accountDataUpdate = function(accounts) {
    call.accounts = accounts;
    var promise = getCallDetails(getCallIdsFromCallsForFilteredAccount())
      .then(function() {
        updateProductFrequencyTableData();
        updateAccountDetailData(true);

        updateDailyFrequencyChartData();
        updateWeeklyFrequencyChartData();
        updateCallFrequencyTableData();
        updateAccountFrequencyData();

        return $timeout(function() {
            $scope.$broadcast('updateDoughnutChart');
            $scope.$broadcast('updateBarChart');
        });
    });

    return promise;
};

然后使用该承诺进行链接:

filter.filterAccountsByProductMetrics1 = function(productWithSegmentations12) {
    return accountService.fetchAccountForRecordType([filter.selectedHcpHco.Name.display])
      .then(function(resp) {  
        return $scope.accountDataUpdate({
            accounts: resp.data
        });
    }).then(function() {
        var productId = null;
        if(filter.selectedMySetupProduct.Product_vod__c) {
            productId = filter.selectedMySetupProduct.Product_vod__c.value;
        }
        return callService.getCallsForProductId(productId)
    }).then(function(calls) {
        filter.filterRecords[filterType.product.value] = calls;
        return $scope.callDataUpdate({
            calls: applyAllFilterOnCalls()
        });
    });
};  

因为调用promise的.then方法会返回一个新的派生promise,所以很容易创建一个promise链。

可以创建任何长度的链,并且由于可以使用另一个承诺(将进一步推迟其解析)来解决承诺,因此可以在链中的任何点暂停/推迟承诺的解析。这使得实现强大的API成为可能。

有关更多信息,请参阅AngularJS $q Service API Reference - Chaining promises

© www.soinside.com 2019 - 2024. All rights reserved.