承诺解决$ transitions onstart hook

时间:2018-03-13 10:52:11

标签: javascript angularjs angular-ui-router angular-promise es6-promise

我正在尝试实现一种场景,其中在远离特定路线(AngularJS UI-router)之前警告用户,并且基于用户选择(是/否),路线转换被继续或中止。
我正在尝试使用$ transitions onStart生命周期钩子来跟踪路由更改。根据UI路由器文档https://ui-router.github.io/guide/transitionhooks#promises,我将返回一个从其他控制器方法解析的promise(当用户在警告弹出窗口中选择一个选项时调用该方法)。但是当用户在弹出窗口中选择一个选项时,则在控制器方法内部 - navigateAway()& stay()解析函数未定义。

有人可以帮忙解决这个问题吗?

function HeaderController($state, authService, $log, $transitions) {
    var vm = this;
    angular.element('.navigateOfferModal').modal();
    $transitions.onStart({from: 'app.location'}, function (transition) {
     if (transition.from().name === 'app.location') {
      angular.element('.navigateOfferModal').modal('open');
      return new Promise(function (resolve, reject) {
        vm.outsideResolve = resolve;// These lines are executed as checked with debugger
        vm.outsideReject = reject;// These lines are executed as checked with debugger
      });
    }
   });

   function navigateAway() {
      angular.element('.navigateOfferModal').modal('close');
      vm.outsideResolve(); // Gives ERROR outsideResolve is undefined
   }

   function stay() {
      angular.element('.navigateOfferModal').modal('close');
      vm.outsideReject(); // Gives ERROR outsideReject is undefined
   }

0 个答案:

没有答案