简体   繁体   English

完成对角度服务的异步调用后,调用控制器功能

[英]Call controller function after async calls to angular service have completed

I'm using the Angular Bootstrap Calendar with a custom cell template and cell modifier . 我正在将Angular Bootstrap Calendar与自定义单元模板cell修改器一起使用 In my controller, I need to get some config data from a service that is used in the cellModifier function before the modifier function is called. 在我的控制器中,需要在调用修饰符函数之前从cellModifier函数中使用的服务中获取一些配置数据。

(function() {
  angular.module('formioAppBasic')
    .controller('calendarController', function($scope, moment, calendarConfig, $http, Formio, shiftService, AppConfig) {

    var vm = this;

    calendarConfig.templates.calendarMonthCell = 'views/calendar/dayTemplate.html';
    calendarConfig.dateFormatter = 'moment';

    vm.events = [];
    vm.calendarView = 'month';
    vm.viewDate = moment().startOf('month').toDate();
    vm.appConfig = AppConfig;
    vm.currentUser = Formio.getUser();
    // Get station config.
    shiftService.getStationConfig().then(function(data) {
      vm.configSlots = data;
    });

    shiftService.getShiftsMonth(token, startDate, endDate)
      .then(onShifts, onError);

    var onShifts = function(data) {
      vm.events = data;
    };

    var onError = function(error) {
      vm.error = 'There was an error.';
    };

    var startDate = moment(this.viewDate).toISOString();
    var endDate = moment(this.viewDate).endOf('month').toISOString();

    var endpoint = APP_URL + '/shift/submission';
    var token = Formio.getToken();

    vm.cellModifier = function(cell) {
      // Do work to create the data in the custom cell.
    };

    $scope.$on('$destroy', function() {
      calendarConfig.templates.calendarMonthCell = 'mwl/calendarMonthCell.html';
    });
  });
})();

The config data I need to use is returned from the two calls to the shiftService service, and that part works fine. 我需要使用的配置数据是从对shiftService服务的两次调用中返回的,该部分工作正常。 As shown in the docs , the cell modifier function is called from the mwl-calendar directive. docs所示,从mwl-calendar指令调用单元格修饰符函数。

<h1>Shift Calendar</h1>

<h2 class="text-center">{{ calendarTitle }}</h2>

<ng-include src="'views/calendar/calendarControls.html'"></ng-include>

<mwl-calendar
  view="vm.calendarView"
  view-date="vm.viewDate"
  events="vm.events"
  view-title="calendarTitle"
  cell-is-open="true"
  cell-modifier="vm.cellModifier(calendarCell)"
>
</mwl-calendar>

When I run my code as it is, the cellModifier is called before the calls to shiftService.getStationConfig and shiftService.getShiftsMonth are returned. 当我按原样运行代码时,将在返回对shiftService.getStationConfigshiftService.getShiftsMonth的调用之前调用shiftService.getStationConfig

Considering that the cellModifier is called from outside the controller, how I can I structure my code so that the cellModifier isn't called until the other two calls to shiftService have returned their data? 考虑到cellModifier是从控制器外部调用的,我如何构造代码,以便在另外两次对shiftService的调用返回其数据之前不调用cellModifier?

Thanks. 谢谢。

If cellModifier get called from outside then you obviously have no control over when it's going to be called. 如果从外部调用cellModifier,那么您显然将无法控制何时调用它。 However, the good news is that you don't actually have to have that control. 但是,好消息是您实际上不必控制该控件。 All you need is to postpone doing your stuff until those two internal calls are finished, and this is pretty easy. 您所需要做的就是将您的工作推迟到两个内部调用完成为止,这很容易。

var promise1 = shiftService.getStationConfig();
promise1.then(function(data) {
  vm.configSlots = data;
});

var promise2 = shiftService.getShiftsMonth(token, startDate, endDate);
promise2.then(onShifts, onError);

vm.cellModifier = function(cell) {
    $q.all([promise1, promise2]).then(function () {
        // do your stuff here
    });
};

The only additional thing you need is to inject $q service into your controller. 您唯一需要做的就是将$ q服务注入到控制器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM