带有id的ng-route正在工作,但是带有来自json的id的id是错误的

时间:2016-11-24 12:24:31

标签: javascript php angularjs json

这是main.html结构

<ul ng-repeat = 'user in users'>    
 <li>{{user.first_name}} <span>X</span></li>
 <li>{{user.last_name}}</li>
 <li>{{user.email}}</li>
 <a href="#footer/{{user.id}}"/><li>{{user.id}}</li></a>
 </ul>

这是我的route.js

app.config(function($routeProvider) {
$routeProvider
.when("/footer/:id",{
templateUrl:'./footer.html',
controller: "getCtrl"
})
.when("/nemke",{
templateUrl:"./form.html"
})
.otherwise({
redirectTo:"/"  
})
});
app.controller('secondCtrl', function($scope) {
$scope.name = "nemkesafsafa";

});

这是main.js文件

var app = angular.module("app",['ngRoute']);
app.controller('getCtrl',['$scope', '$http', '$routeParams', 'users', function($scope, $http, $routeParams, users) {
users.success(function(data){
var obje = data[0].first_name;
console.log(obje);
var keys  =  [];
$scope.profile = data[$routeParams.id];

for(var i = 0; i< data.length; i++) {
    keys.push(data[i].id);
}
$scope.users  = data;
});
users.error(function(){
    console.log("Nemke")
})
}]);

json.php

[{"id":"126","first_name":"Nemanja","last_name":"Dukic","email":"Car"},{"id":"127","first_name":"Nemanja","last_name":"Dukic","email":"Car"}]

footer.html

<div>
<h2>{{profile.id}}</h2>
<h2>{{profile.fist_name}}</h2>
<h2>{{profile.last_name}}</h2>
<h2>{{profile.email}}</h2>

</div>

所以问题是当我clikc链接时url很好,但数据dipslay id footer.html是错误的

这是picuter,这样很容易

见下图中的描述。

enter image description here

提前致谢。

1 个答案:

答案 0 :(得分:1)

$ scope.profile = data [$ routeParams.id];

应该被替换

 $scope.profile = data.find(function(ele){
    return ele.id == $routeParams.id
 });