$ routeParams在$ locationChangeSuccess上清空

时间:2014-06-10 03:07:51

标签: javascript angularjs

我在以下运行块中配置我的应用程序。基本上我想要执行一项操作,要求我知道$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);
        });

    }]);

}

访问网址:

http://localhost:8080/landlord/#/rentroll?landlord-account-id=ahlwcm9wZXJ0eS1tYW5hZ2VtZW50LXN1aXRlchwLEg9MYW5kbG9yZEFjY291bnQYgICAgICAgAoM&billing-month=2014-06

3 个答案:

答案 0 :(得分:3)

访问/rentroll,您将$routeParams为空$routeProvider 该路径的配置不期望URL上的任何变量。

$routeParams用于获取网址的变量值。例如:

$routeProvider
.when('/rentroll/:variable', {...});

并在/rentroll/something上访问它会给您$routeParams,如下所示:

$routeParams.variable == 'something';

答案 1 :(得分:3)

这对我有用,在你的回调中:

.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']);
  });
})

答案 2 :(得分:1)

https://docs.angularjs.org/api/ngRoute/service/ $ routeParams

解释一下$ routeParams何时可用及其原因。

此处的解决方案可能是使用$route.current.params

相关问题