AngularJS : requête $http(API REST) fait 2 fois de suite...
bonjour les amis,
j'ai un soucis, à chaque changement de page, ma requête à l'API REST est effectuée 2 fois même parfois 3 fois.
j'ai 3 pages, à chaque page je fais une requête API REST via $http dans une factory pour récupérer des données.
chaque controller fait donc appelle à cette factory et passe les données à la vue.
la factory Product:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
'use strict';
angular.module('starter')
.factory('Product', function($http) {
Product.products = [];
Product.getAll = function() {
return $http.get('http://apirest-xxxxxxxx/api/v1/produits') // FAIT 2 fois
.success(function(data) {
Product.products = data;
});
};
...
return Product;
}); |
mon controller :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
'use strict';
starter.controller('ProductShowAllController', function($scope, Product) {
this.products = Product.products;
Product.getAll()
.then(function() {
this.products = Product.products;
console.log(this.products); //
}.bind(this));
... |
app.js avec les routes :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
var starter = angular.module('starter', ['ngRoute']);
starter.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/home', {
templateUrl: 'views/home.html',
controller: 'HomeController'
}).
when('/produits', {
templateUrl: 'views/produits.html',
controller: 'ProductShowAllController'
}).
.... |
via firebug, je regarde donc le reseau et je vois à chaque changement de page; 2 fois la même requête effectuée. :(
et console.log(this.products); est exécuté 2 fois, j'ai 2 lignes
une idée ?