首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$routeParams空在$locationChangeSuccess上

$routeParams空在$locationChangeSuccess上
EN

Stack Overflow用户
提问于 2014-06-10 03:07:50
回答 3查看 2.6K关注 0票数 1

我在下面的运行块中配置我的应用程序。基本上,我想预先形成一个动作,要求我知道每个$routeParams$locationChangeSuccess

然而,$routeParams 在这一点上是空的!有工作回合吗?到底怎么回事?

代码语言:javascript
复制
app.run(['$routeParams', function ($routeParams) {

    $rootScope.$on("$locationChangeSuccess", function () {
        console.log($routeParams);
    });

}]);

更新

代码语言:javascript
复制
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:

代码语言:javascript
复制
http://localhost:8080/landlord/#/rentroll?landlord-account-id=ahlwcm9wZXJ0eS1tYW5hZ2VtZW50LXN1aXRlchwLEg9MYW5kbG9yZEFjY291bnQYgICAgICAgAoM&billing-month=2014-06
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-06-10 05:48:02

这对我有效,在你的运行回调中:

代码语言:javascript
复制
.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']);
  });
})
票数 3
EN

Stack Overflow用户

发布于 2014-06-10 03:25:30

通过访问/rentroll,您将得到空的$routeParams,因为您对该路径的$routeProvider配置并不期望URL上有任何变量。

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

代码语言:javascript
复制
$routeProvider
.when('/rentroll/:variable', {...});

/rentroll/something上访问它将给您$routeParams如下所示:

代码语言:javascript
复制
$routeParams.variable == 'something';
票数 3
EN

Stack Overflow用户

发布于 2014-06-10 04:00:55

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

解释$routeParams何时可用以及原因。

这里的解决方案可能是使用$route.current.params

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24132232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档