angularjs – 如何在Angular UI路由器中访问$urlRouterProvider的’otherwise’属性

前端之家收集整理的这篇文章主要介绍了angularjs – 如何在Angular UI路由器中访问$urlRouterProvider的’otherwise’属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在某些情况下,我想导航到默认或“其他”状态.所以我需要从控制器获取路由对象中配置的其他状态.

当我尝试在我的指令的控制器中注入$urlRouterProvider时,我收到一个错误

Error: [$injector:unpr] Unknown provider: $stateProviderProvider <-
$stateProvider <- bbvNavDirective

我究竟做错了什么?

示例代码

.config([ '$stateProvider','$urlRouterProvider','$locationProvider',( $stateProvider,$urlRouterProvider,$locationProvider ) => {

    $locationProvider.html5Mode(true);
    $urlRouterProvider.otherwise('/bbv/voorstellen/open');

    (...routing code)

}])

指示:

.directive('bbvNav',[ '$state',( $state ) => {
        return {
            restrict: 'E',template,scope: {
                titleBind: '&',onButtonClick: '&'
            },bindToController: true,controller: [ '$scope',function ( $scope ) {
                $state.go( <NAVIGATE TO URLROUTERPOVIDER.OTHERWISE> );
                console.log('Accessing $urlRouterProvider.otherwise state:'); // How do I get the value of $urlRouterProvider.otherwise here?
            }
        }
    })

解决方法

我在这种情况下使用这种方法.
首先,我总是把我的默认状态设置为常量,所以每当我需要它时,我只是向控制器或服务注入常量以使用它…

不变

app.module('example').constant('defaultState','app.defaultState')

然后首先将其注入您的配置文件并与$urlprovider一起使用

CONFIG

$urlRouterProvider.otherwise(function ($injector,defaultState) {
    var $state = $injector.get('$state');
    $state.go(defaultState);
});

所以常常只有注入你想要的任何组件并使用它…

控制器示例

.controller(defaultState){
   ... TODO ...
}

猜你在找的Angularjs相关文章