Angular.js - 获得通过$路由或$ routeParams访问PARAMS [英] Angular.js - getting access to the params via $route or $routeParams

查看:172
本文介绍了Angular.js - 获得通过$路由或$ routeParams访问PARAMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我注入$路径和$ routeParams而是一个控制器,当我去通过

要获取值

$ route.routes.current.params.test - >>当前未定义
$ routeParams.test - >>测试未定义

这两个对象似乎当我使用的console.log($路径)或执行console.log($ routeParams)要正确填充

我很困惑。值怎么会在那里的console.log但不能说我做从的console.log相同的控制器里面呢?

更新:样品code

  angular.module('TestApp)
        .controller('的TestController',['$范围,$ HTTP,$ routeParams','$路线',
            功能($范围,$ HTTP,$ routeParams,$路线){
                的console.log($ routeParams);
                //console.log($routeParams.test);
                的console.log($ route.current.test);
                //console.log($route.routes);            }]);


解决方案

请参阅http://deansofer.com/posts/view/14/AngularJs-Tips-and-Tricks-UPDATED#routing和<一个href=\"https://groups.google.com/d/msg/angular/ib2wHQozeNE/sC3SX3QTyRgJ\">https://groups.google.com/d/msg/angular/ib2wHQozeNE/sC3SX3QTyRgJ

在您的code,$路线和$ routeParams很可能被异步解决。到时候你展开控制台的对象,他们已经解决了,但他们都没有解决控制器构造函数运行时。

使用 $范围。在$('$ routeChangeSuccess',函数(...){...})在你的控制器,并试图检查路径属性那里。

<一个href=\"http://docs.angularjs.org/api/ngRoute.%24route#%24routeChangeSuccess\">$routeChangeSuccess

I have an Controller in which I inject $route and $routeParams but when I go to get the value via

$route.routes.current.params.test ->> current is undefined $routeParams.test ->> test is undefined

Both objects seem to be populated correctly when I use console.log($route) or console.log($routeParams)

I'm baffled. How could the value be there for console.log but fail inside the same controller that I doing the console.log from?

Update: Sample Code

    angular.module('TestApp')
        .controller('TestController', ['$scope', '$http', '$routeParams', '$route',
            function($scope, $http, $routeParams, $route) {
                console.log($routeParams);
                //console.log($routeParams.test);
                console.log($route.current.test);
                //console.log($route.routes);

            }]);

解决方案

See http://deansofer.com/posts/view/14/AngularJs-Tips-and-Tricks-UPDATED#routing and https://groups.google.com/d/msg/angular/ib2wHQozeNE/sC3SX3QTyRgJ

In your code, $route and $routeParams are likely being resolved asynchronously. By the time you expand the objects in the console, they have been resolved, but they are not resolved when the controller constructor function runs.

Use $scope.$on('$routeChangeSuccess', function(...) { ... }) in your controller, and try examining the route properties there.

$routeChangeSuccess

这篇关于Angular.js - 获得通过$路由或$ routeParams访问PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆