2016-01-11 150 views
0

使用URL我有一個狀態:捕獲和角度UI路由器

.state('series', { 
    url: '/{series}/', 
    template: '<div configurator-list></div>', 
    resolve: { 
     stateService: 'ConfiguratorStateService' 
    }, 
    params: { 
     series: '{series}' 
    }, 
    controller: 'ConfiguratorListController', 
    ncyBreadcrumb: { 
     label: '{series}', 
     parent: 'home' 
    } 
}); 

我想使用{}系列是在URL中更新了一些東西實際價值。我迷路了,沒有找到任何運氣。一切都將我帶到UI路由器頁面,但我沒有看到任何具體的例子。

回答

1

您需要偵聽任何UI-Router狀態更改事件並使用該功能的參數來獲取它。在其上下文詳述您的應用程序運行(「對myApp」替代應用程序名稱)添加監聽...

(function() { 
    angular.module('myApp') 
     .run(runOptions); 

runOptions.$inject = ['$rootScope', '$state']; 

function runOptions($rootScope, $state) { 
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { 

     if (toState.name === 'series') { 
      //output the parameter here... since the series parameter is actually the string of the url, use the url property 
      console.log(toState.url); 
     } 
    }); 
} 
})(); 

有您可以使用其他狀態更改事件爲好,這取決於你的使用目的:https://github.com/angular-ui/ui-router/wiki#state-change-events

OR

什麼聲音與你的問題的進一步閱讀更具相關性,修改你的路由定義...

變更網址: '/ {}系列/' 到 '/:系列'

然後,在您的控制器中......

angular.controller('ConfiguratorListController', function($stateParams) { 
    var vm = this; 
    vm.series = $stateParams.series; 
}