我正在寻找在AngularJS中使用哈希标记标识符的最简单的解决方案。 我正在使用AngularJS版本1.2.22
我的代码如下:
的App.config
// in app config:
$routeProvider.when("/:page_id", {template: '', controller: "PageController"});
控制器
// in PageController - where I would like to read the most recent #value
$scope.$on('$routeChangeSuccess', function(event) {
// What do I grab here?
/* I have tried the followings
- $routeParams.page_id -> keeps previous value (documentation says too it will)
- $route.current.params.page_id (current is undefined when trying to access)
*/
});
网址更改代码
// my trigger code is a simple $location.path()
$location.path('/'+page_id);
$(
答案 0 :(得分:0)
我建议你使用resolve属性(plunker):
$routeProvider.when('/:page_id', {
template: '<pre>{{page| json}}</pre>',
controller: 'PageController',
resolve: {
page: function($route, $q) {
var id = $route.current.params.page_id;
var promise = $q.when({ id: id }); // fetch the page data from the server
return promise;
}
}
});
然后将它注入你的控制器:
app.controller('PageController', function($scope, page) {
$scope.page = page;
});