AngularJs : récupération de donnée via un json

Résolu/Fermé
xander18 Messages postés 68 Date d'inscription dimanche 24 février 2013 Statut Membre Dernière intervention 21 janvier 2015 - 17 mars 2014 à 15:26
xander18 Messages postés 68 Date d'inscription dimanche 24 février 2013 Statut Membre Dernière intervention 21 janvier 2015 - 17 mars 2014 à 16:50
Bonjour,
Je travaille actuellement sur Angular JS et j'ai une erreur que j'arrive pas a résoudre, je m'explique :
j'ai un fichier json qui ressemble a ceci :
[
{
"nid": "24",
"vid": "24",
"type": "page",
"language": "fr",
"title": "Presse",
"uid": "1",
"status": "1",
"created": "1390404181",
"changed": "1390404181",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/24"
}
ou j'ai plusieurs articles, j'ai commencé par afficher tout les titres des articles en lien jusqu'ici aucun problème, mon problème se situe lorsque je veut récupéré un article et ces informations par rapport a son 'nid' en cliquant sur le lien du titre.

Du coup j'ai testé plusieurs log pour voir se que je récupérais, et il apparemment je récupère un undefined alors que mon routeParams.nid récupère bien le nid de mon objet mais il s'affiche d'abord en undefined puis ensuite il obtiens la valeur du nid. some help plz voici mon code:


<!-- Script JS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script src="https://code.angularjs.org/1.2.14/angular-sanitize.js"></script>
<script src="https://code.angularjs.org/1.2.14/angular-route.js"></script>

<script>
// mon module app contient les module de Route et de Sanitize
var app = angular.module('app', ['ngSanitize', 'ngRoute'])


// systeme de routing
app.config(function($routeProvider){
$routeProvider
.when('/home', {templateUrl: 'partials/home.html', controller: 'contentsCtrl'})
.when('/test/:nid', {templateUrl: 'partials/test.html', controller: 'contentsCtrl'})
.otherwise({redirectTo : '/'});
})

app.factory('ContentFactory', function(){
var factory = {
contents : [
{
"nid": "24",
"vid": "24",
"type": "page",
"language": "fr",
"title": "Presse",
"uid": "1",
"status": "1",
"created": "1390404181",
"changed": "1390404181",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/24"
},
{
"nid": "23",
"vid": "23",
"type": "page",
"language": "fr",
"title": "O\u00f9 acheter nos produits ?",
"uid": "1",
"status": "1",
"created": "1390305089",
"changed": "1394450294",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/23"
},
{
"nid": "22",
"vid": "22",
"type": "page",
"language": "fr",
"title": "Agr\u00e9ment",
"uid": "1",
"status": "1",
"created": "1386698804",
"changed": "1386698804",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/22"
},
{
"nid": "21",
"vid": "21",
"type": "diaporama",
"language": "fr",
"title": "Le contr\u00f4le Achahada",
"uid": "1",
"status": "1",
"created": "1386674723",
"changed": "1386681904",
"comment": "0",
"promote": "1",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/21"
},
{
"nid": "20",
"vid": "20",
"type": "diaporama",
"language": "fr",
"title": "Bourget 2013",
"uid": "1",
"status": "1",
"created": "1386674103",
"changed": "1386674103",
"comment": "0",
"promote": "1",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/20"
},
{
"nid": "19",
"vid": "19",
"type": "diaporama",
"language": "fr",
"title": "Halal Expo 2013",
"uid": "1",
"status": "1",
"created": "1386672569",
"changed": "1386672902",
"comment": "0",
"promote": "1",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/19"
},
{
"nid": "18",
"vid": "18",
"type": "page",
"language": "fr",
"title": "La tra\u00e7abilit\u00e9",
"uid": "1",
"status": "1",
"created": "1386611718",
"changed": "1386611718",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/18"
},
{
"nid": "17",
"vid": "17",
"type": "page",
"language": "fr",
"title": "La d\u00e9coupe et la transformation",
"uid": "1",
"status": "1",
"created": "1386611363",
"changed": "1386611363",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/17"
},
{
"nid": "16",
"vid": "16",
"type": "page",
"language": "fr",
"title": "Le sacrifice",
"uid": "1",
"status": "1",
"created": "1386611319",
"changed": "1386611319",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/16"
},
{
"nid": "15",
"vid": "15",
"type": "page",
"language": "fr",
"title": "Le contr\u00f4le",
"uid": "1",
"status": "1",
"created": "1386611222",
"changed": "1386611345",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/15"
},
{
"nid": "14",
"vid": "14",
"type": "page",
"language": "fr",
"title": "L'association Achahada",
"uid": "1",
"status": "1",
"created": "1381422338",
"changed": "1386610438",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/14"
},
{
"nid": "13",
"vid": "13",
"type": "page",
"language": "fr",
"title": "Nos engagements",
"uid": "1",
"status": "1",
"created": "1381421655",
"changed": "1381421655",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/13"
},
{
"nid": "12",
"vid": "12",
"type": "page",
"language": "fr",
"title": "L'\u00e9lectronarcose",
"uid": "1",
"status": "1",
"created": "1381421345",
"changed": "1381421345",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/12"
},
{
"nid": "11",
"vid": "11",
"type": "page",
"language": "fr",
"title": "Histoire",
"uid": "1",
"status": "1",
"created": "1381420332",
"changed": "1386610097",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/11"
},
{
"nid": "10",
"vid": "10",
"type": "page",
"language": "fr",
"title": "Vid\u00e9os",
"uid": "1",
"status": "1",
"created": "1381419455",
"changed": "1381419455",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/10"
},
{
"nid": "9",
"vid": "9",
"type": "page",
"language": "fr",
"title": "Photos",
"uid": "1",
"status": "1",
"created": "1381419425",
"changed": "1386615025",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/9"
},
{
"nid": "8",
"vid": "8",
"type": "page",
"language": "fr",
"title": "Restaurants",
"uid": "1",
"status": "1",
"created": "1381419241",
"changed": "1386612930",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/8"
},
{
"nid": "7",
"vid": "7",
"type": "page",
"language": "fr",
"title": "Boucherie",
"uid": "1",
"status": "1",
"created": "1381419128",
"changed": "1386612544",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/7"
},
{
"nid": "6",
"vid": "6",
"type": "page",
"language": "fr",
"title": "Fournisseur",
"uid": "1",
"status": "1",
"created": "1381419064",
"changed": "1386612386",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/6"
},
{
"nid": "5",
"vid": "5",
"type": "page",
"language": "fr",
"title": "Centre d'Elaboration des Viandes",
"uid": "1",
"status": "1",
"created": "1381337392",
"changed": "1386612838",
"comment": "0",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uri": "http://achahada.fr/json/articles/node/5"
}
],

getContents : function(){
return factory.contents;
},
getContent : function(nid){
var content = {};
angular.forEach(factory.contents, function(value, key){
if(value.nid == nid){
content = value
}
});
return content;
},
}
return factory;
})


app.controller('contentsCtrl', function ($scope, ContentFactory, $routeParams){

var content = ContentFactory.getContents($routeParams.nid);
$scope.contents = ContentFactory.getContents();
// $scope.title = content[8].title;
console.log(content);
console.log($routeParams.nid);
//test
console.log(content[0].title);
});

</script>



1 réponse

xander18 Messages postés 68 Date d'inscription dimanche 24 février 2013 Statut Membre Dernière intervention 21 janvier 2015 2
17 mars 2014 à 16:50
Résolue j'avais une erreur j'appelais la mauvaise fonction
0