Angular-ui-router state with $stateParams always redirects to $urlRouteProvider.otherwise

↘锁芯ラ 提交于 2019-12-11 12:43:38

问题


I'm using Angular-ui-router's $stateProvider to navigate in my app. The routes are defined like so (only relevant parts are shown):

function config($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/dashboard');

    $stateProvider
        .state('common', {
            url: '/',
            abstract: true,
            templateUrl: folderDictionary.common + 'content.html'
        })
        .state('common.dashboard', {
            url: 'dashboard',
            templateUrl: folderDictionary.dashboard + 'dashboard.html',
            controller: 'DashboardController'
        })
        .state('common.news', {
            url: 'news',
            templateUrl: folderDictionary.news + 'news.html',
            controller: 'NewsController'
        })
        .state('common.news_edit', {
            url: 'news/edit',
            templateUrl: folderDictionary.news + 'news_edit.html',
            controller: 'NewsEditController'
            params: {newsItem:null}
        })
        .state('common.news_add', {
            url: 'news/add',
            templateUrl: folderDictionary.news + 'news_edit.html',
            controller: 'NewsEditController'
        });
}

config.$inject = ['$stateProvider', '$urlRouterProvider'];

angular
    .module('admin.core')
    .config(config);

Navigating to the different news-states is handled in the controllers using $state.go():

$scope.edit = function(id) {
    var item = $filter('filter')($scope.news, { Id: id })[0];
    $state.go('common.news_edit', { newsItem: item });
}

$scope.add= function() {
    $state.go('common.news_add');
}

All states work fine, except for common.news_edit. Whenever I try to navigate to that route, it redirects to the route defined in $urlRouterProvider.otherwise().

When I remove $urlRouterProvider.otherwise(), the common.news_edit state works.

I suppose the $stateParams are causing this, but I'm not sure how to solve this? I don't want to pass the newsItem object in the url.

来源:https://stackoverflow.com/questions/37511545/angular-ui-router-state-with-stateparams-always-redirects-to-urlrouteprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!