繁体   English   中英

从app.js触发$ modal.open

[英]Trigger $modal.open from app.js

我的app.js看起来像这样。 我需要全局处理AJAX错误。 如果我只使用console.log,这段代码工作正常。 但是,我需要使用$ modal触发模态对话框,但不知道如何实现它。
任何帮助表示赞赏。 谢谢!

define(['jquery','angularAMD', 'modernizr' ,'angular-ui-router'], function ($, angularAMD) {
var app = angular.module('ABC', ['ui.router','angular.css.injector']),
ngAMD = angularAMD(app);
app.run(function($rootScope){
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from) {
    $rootScope.previousState = from.name;
    $rootScope.currentState = to.name;
  });
});

app.config(function($stateProvider, $urlRouterProvider, $provide, $httpProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider
.state('first',
      ngAMD.route({
        url:'/first',
        templateUrl: 'app/partials/first.html',
        controller : 'firstCrtl'
      })
  )
.state('default',
          ngAMD.route({
            url:'/',
            templateUrl: 'app/partials/default.html',
            controller : 'defaultCrtl'
      })
 );

$provide.factory('globalAjaxInterceptor', function($q) {

  return {

    // On request success
    request: function(config) {
      return config || $q.when(config);
    },

    // On request failure
    requestError: function(rejection) {
      return $q.reject(rejection);
    },

    // On response success
    response: function(response) {
      if(response.data.status === "FAILURE" && response.data.failueCode === "F003") {
          console.log('Trigger Modal');
      }else if(response.data.status == "SUCCESS"){
          console.log('Response Address Successs');

      }

      // Return the response or promise.
      return response || $q.when(response);

   },

    // On response failture
    responseError: function(rejection) {
      // Return the promise rejection.
        switch (rejection.status) {
          case 404:
              console.log('Destinaion not found!')
            break;
          default:
            console.log('Trigger Modal');
        }
        return $q.reject(rejection);
    }
  };
});

// Add the interceptor to the $httpProvider.
$httpProvider.interceptors.push('globalAjaxInterceptor');
  });
  ngAMD.bootstrap();
  return app;
});

他的问题的解决方案是修复Circular dependency errorCdep的解决方案。 修复是使用$injector明确注入$modal 解决方案如下所示 -

responseError: function(rejection) {
      // Return the promise rejection.
      switch (rejection.status) {
        case 404:
          console.log('Destinaion not found!')
          break;
        default:
          $injector.invoke(function($modal) {
            $rootScope.errorOverlay($modal);
          });
      }
      return $q.reject(rejection);
    }

暂无
暂无

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

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