Javascript Angular JS使用app.config/UI-Router-oneter中的自定义提供程序

Javascript Angular JS使用app.config/UI-Router-oneter中的自定义提供程序,javascript,angularjs,dependency-injection,Javascript,Angularjs,Dependency Injection,我想在OneNet事件处理程序中为ui路由器状态调用提供程序上的方法。调用this.$get函数中返回的方法时遇到问题。我得到一个错误,该方法未定义 供应商: (function () { 'use strict'; angular.module('my.module') .provider('mpSvc', function () { this.$get = function ($state) { retu

我想在OneNet事件处理程序中为ui路由器状态调用提供程序上的方法。调用this.$get函数中返回的方法时遇到问题。我得到一个错误,该方法未定义

供应商:

(function () {
    'use strict';
    angular.module('my.module')
        .provider('mpSvc', function () {
            this.$get = function ($state) {
                return {
                    setBreakpoint: function () {
                        var currentState = $state.$current.self.name;
                        //other processing here
                    }
                }
            };

            this.myMethod = function() {
                console.log('Im defined but I cant access $state')
                console.log('$state is ', $state);
            }
        });
})();
应用程序配置:

angular.module('app', [
    'ui-router',
    'my.module'
])
    .config(['$urlRouterProvider', '$stateProvider', 'mpSvcProvider',
        function ($urlRouterProvider, $stateProvider, mpSvcProvider) {

            // Default route
            $urlRouterProvider.otherwise('/');

            $stateProvider
                .state('main', {
                    url: '/',
                    templateUrl: 'main/main-view.html',
                    controller: 'MainCtrl'
                })

                .state('main.some.other.state', {
                    abstract: true,
                    url: 'someurl/',
                    templateUrl: 'my-template.html',
                    controller: 'MyCtrl',
                    onEnter: function() {
                        console.log('enter mp');
                        mpSvcProvider.myMethod();  //this is visible
                        mpSvcProvider.setBreakpoint();  //this is not visible
                    }
                });
        }]);


结果是我看错了问题。当启动oneter/onExit时,我们实际上已经不在配置代码块中了,服务可以被定向(san名称中的“提供者”)

e、 g:

$stateProvider
    .state('main.some.other.state', {
                        abstract: true,
                        url: 'someurl/',
                        templateUrl: 'my-template.html',
                        controller: 'MyCtrl',
                        onEnter: function(mpSvc) {
                            console.log('enter mp');
                            mpSvc.setBreakpoint();  //this is now visible
                        }
                    });