$ routeParams AngularJs没有给Id回复

时间:2015-04-19 12:43:17

标签: angularjs-routing

我试图找出它如何使用$ routeParams的角度,我有一个像这样的json对象: [ { "pId": "DE123", "name": "Gogo", "email": "gogo@gogo.de", "roles": "Administrator" },{ "pId": "DE124", "name": "Giant Ape", "email": "giant@ape.de", "roles": "Stuff" },{ "pId": "DE125", "name": "Jonnie", "email": "jonnie@goodboy.de", "roles": "Redaktur" },{ "pId": "DE126", "name": "Supermen", "email": "supermen@isdead.de", "roles": "Redaktur" } ]

我的控制器是:

.controller('usersCtrl', function($scope, $http, $routeParams){
$scope.msg = 'I come from Users Controller';
$http.get('dataModel/users.json').then(function(res){
    $scope.users = res.data;
    $scope.users = $routeParams.pId;
})

})

我的路线是:

        .when('/users/:pId', {
        templateUrl: 'tpl/viewUsr.html',
        controller: 'usersCtrl'
    })

和我的tpl / viewUsr.html是:

<h3>Hello User {{users[0].pId}}</h3>
<p>i come from {{users[0].pId}}</p>

某种程度上,视图没有从Controller获得pId。我做错了什么?非常感谢您的任何想法或帮助。

1 个答案:

答案 0 :(得分:0)

解决!!

我们走了。我在控制器上做错了它应该是这样的。

.controller('usersCtrl', ['$scope', '$http', '$routeParams', function($scope, $http, $routeParams){
$scope.msg = 'I Come from Users Controller';
$http.get('dataModel/users.json').success(function(res){
    $scope.users = res;
    $scope.viewUsr = $routeParams;
    //$routeParams = res.data.pId;
}) }])

并在视图中:

<h3>View User #{{viewUsr.pId}}</h3>
<p>Hello User #{{viewUsr.pId}}</p>

如果有人遇到与我一样的问题,请玩得开心:)感谢所有人为我的问题做了些什么。 :)