Bonjour, je m'expérimente un peut en angular js en attendant de récupérer mon Pc pour traiter un autres sujet.
J'aimerais savoir en utilisant les factory, si il étais possible de récupérer des données d'un json mais uniquement si celles ci corresponde à une catégorie donnée
un truc du genre :
Controller
// Art Direction page controller
lupus.controller('ArtDirectionController', function($scope, ProjectFactory) {
$scope.viewClass = "slideleft art";
$scope.currentCategory = "Art Direction";
var category = 'art-direction';
$scope.projects = ProjectFactory.getProjects(category);
console.log($scope.projects);
});
View :
je veut afficher les projets mais uniquement ceux correspondant à une catégorie (ici la catégorie 'art-direction)
<div class="projects">
<div class="project" ng-repeat="project in projects">
<a href="#project/{{project.id}}">
<img src="{{project.project_image}}" alt="project image" width="600" height="400">
</a>
</div>
</div>
Factory
// Factory ============================================
lupus.factory('ProjectFactory', function(){
var factory = {
projects : [
{
id: '1',
project_name: 'Montaigne',
project_image: 'projects/mon1.jpg',
project_description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia unde voluptatem hic, quibusdam voluptas maiores veritatis repudiandae corporis, repellat saepe, voluptates iste? Minima similique laboriosam est dignissimos blanditiis autem fuga.',
project_category: 'art-direction',
project_images: [
{
id: '1',
name: 'projects/mon2.jpg'
},
{
id: '2',
name: 'projects/mon3.jpg'
}
]
},
{
id: '2',
project_name: 'Boissiere',
project_image: 'projects/b1.jpg',
project_description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia unde voluptatem hic, quibusdam voluptas maiores veritatis repudiandae corporis, repellat saepe, voluptates iste? Minima similique laboriosam est dignissimos blanditiis autem fuga.',
project_category: 'art-direction',
project_images: [
{
id: '1',
name: 'projects/b2.jpg'
},
{
id: '2',
name: 'projects/b3.jpg'
}
]
},
{
id: '3',
project_name: 'Obu',
project_image: 'projects/o1.jpg',
project_description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia unde voluptatem hic, quibusdam voluptas maiores veritatis repudiandae corporis, repellat saepe, voluptates iste? Minima similique laboriosam est dignissimos blanditiis autem fuga.',
project_category: 'art-direction',
project_images: [
{
id: '1',
name: 'projects/o2.jpg'
},
{
id: '2',
name: 'projects/o3.jpg'
}
]
},
{
id: '4',
project_name: 'Heart',
project_image: 'projects/c1.jpg',
project_description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia unde voluptatem hic, quibusdam voluptas maiores veritatis repudiandae corporis, repellat saepe, voluptates iste? Minima similique laboriosam est dignissimos blanditiis autem fuga.',
project_category: 'tatto',
project_images: [
{
id: '1',
name: 'projects/c2.jpg'
}
]
},
{
id: '5',
project_name: 'sunny',
project_image: 'projects/jp1.jpg',
project_description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia unde voluptatem hic, quibusdam voluptas maiores veritatis repudiandae corporis, repellat saepe, voluptates iste? Minima similique laboriosam est dignissimos blanditiis autem fuga.',
project_category: 'tatto',
project_images: [
{
id: '1',
name: 'projects/jp2.jpg'
},
{
id: '2',
name: 'projects/jp3.jpg'
}
]
}
],
getProjects : function(category){
var projects = {};
angular.forEach(factory.projects, function(value, key){
if(value.project_category == category){
projects = value;
}
});
return projects;
// return factory.projects;
},
getProject : function(id){
var project = {};
angular.forEach(factory.projects, function(value, key){
if(value.id == id){
project = value;
}
});
return project;
},
}
return factory;
});
j'ai essayé ceci mais sa ne fonctionne pas quelqu'un aurais des indices Merci ;)
j'ai réussi, voila la solution pour ceux qui pourrait être intéressé. ;)
getProjects : function(category){
var datas = factory.projects;
var projects = [];
for (var i=0; i<datas.length; i++){
if (datas[i].project_category == category) {
projects.push(datas[i]);
}
}
return projects;
},