我在下面的运行块中配置我的应用程序。基本上,我想预先形成一个动作,要求我知道每个$routeParams的$locationChangeSuccess。
然而,$routeParams 在这一点上是空的!有工作回合吗?到底怎么回事?
app.run(['$routeParams', function ($routeParams) {
$rootScope.$on("$locationChangeSuccess", function () {
console.log($routeParams);
});
}]);更新
function configureApp(app, user) {
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/rentroll', {
templateUrl: 'rent-roll/rent-roll.html',
controller: 'pwRentRollCtrl'
}).
when('/bill', {
templateUrl: 'bill/bill/bill.html',
controller: 'pwBillCtrl'
}).
when('/fileroom', {
templateUrl: 'file-room/file-room/file-room.html',
controller: 'pwFileRoomCtrl'
}).
when('/estate-creator', {
templateUrl: 'estate/creator.html'
}).
when('/estate-manager', {
templateUrl: 'estate/manager.html',
controller: 'pwEstateManagerCtrl'
}).
when('/welcomepage', {
templateURL: 'welcome-page/welcome-page.html',
controller: 'welcomePageCtrl'
}).
otherwise({
redirectTo: '/welcomepage'
});
}]);
app.run(['$rootScope', '$routeParams', 'pwCurrentEstate','pwToolbar', function ($rootScope, $routeParams, pwCurrentEstate, pwToolbar) {
$rootScope.user = user;
$rootScope.$on("$locationChangeSuccess", function () {
pwToolbar.reset();
console.log($routeParams);
});
}]);
}访问URL:
http://localhost:8080/landlord/#/rentroll?landlord-account-id=ahlwcm9wZXJ0eS1tYW5hZ2VtZW50LXN1aXRlchwLEg9MYW5kbG9yZEFjY291bnQYgICAgICAgAoM&billing-month=2014-06发布于 2014-06-10 05:48:02
这对我有效,在你的运行回调中:
.run(function($rootScope, $routeParams, $location) {
$rootScope.$on("$locationChangeSuccess", function() {
var params = $location.search();
console.log(params);
console.log('landlord-account-id:', params['landlord-account-id']);
console.log('billing-month', params['billing-month']);
});
})发布于 2014-06-10 03:25:30
通过访问/rentroll,您将得到空的$routeParams,因为您对该路径的$routeProvider配置并不期望URL上有任何变量。
$routeParams用于获取URL的变量值。例如:
$routeProvider
.when('/rentroll/:variable', {...});在/rentroll/something上访问它将给您$routeParams如下所示:
$routeParams.variable == 'something';发布于 2014-06-10 04:00:55
https://docs.angularjs.org/api/ngRoute/service/$routeParams
解释$routeParams何时可用以及原因。
这里的解决方案可能是使用$route.current.params
https://stackoverflow.com/questions/24132232
复制相似问题