2016-11-24 64 views
0

這是main.html中結構ID爲NG-路由工作,但與JSON的ID是錯的

<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的網址是好的,但數據dipslay ID footer.html是錯誤的

下面是它的easire這樣

的picuter在下面的圖像

見詳細描述。

enter image description here

在此先感謝。

+0

你是什麼footer.html而且看起來像$ scope.profile =數據[$ routeParams.id]你assiging基於索引元素應該不是被filterted的ID? –

+0

哪裏是footer.html –

+0

對不起更新答案頁腳@VinodLouis – NemanjaD

回答

1

$ scope.profile =數據[$ routeParams.id];

應更換

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

非常感謝你這偉大工程。 – NemanjaD

相關問題