NemanjaD NemanjaD - 13 days ago 7
PHP Question

ng-route with id is working but id with id from json is wrong

This is main.html structure

<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>


This is my 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";

});


This is main.js file

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>


So the problem is when i clikc on link the url is fine, but the data dipslay id footer.html is wrong

Here's the picuter it's easire this way

See discription in below image.

enter image description here

Thanks in advance.

Answer

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

should be replaced

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