I'm running an app where the navigation includes a multilingual link that triggers a state change.
The problem that I'm running into, is that on every state change from one language to the other, the array comming from the state resolve, gets duplicated.
For example:
- First load = 8 items
- First state change = 16 items
- Second state change= 24 items
- ...
I've created a Plunker to replicate the issue.
Here's my state code:
.config(function($stateProvider) {
$stateProvider.state('fair', {
url: '/fair/{fair}',
resolve: {
fair: function(SearchService, $stateParams) {
var string = $stateParams.fair.replace(/-/g , "");
var finalstring = string.replace(/liste/g , " ");
return SearchService.getAllExhibitors(finalstring);
}
},
views: {
'header': {
templateUrl: 'header.htm',
controller:'appCtrl'
},
'main':{
templateUrl: 'fair.htm',
controller: 'appCtrl'
}
}
})
.state('fairde', {
url: '/de/fair/{fair}',
resolve: {
fair: function(SearchService, $stateParams) {
var string = $stateParams.fair.replace(/-/g , "");
var finalstring = string.replace(/liste/g , " ");
return SearchService.getAllExhibitors(finalstring);
}
},
views: {
'header': {
templateUrl: 'headerde.htm',
controller:'appCtrl'
},
'main':{
templateUrl: 'fairde.htm',
controller: 'appCtrl'
}
}
})
And the factory:
.factory("SearchService", function($http, $q) {
var service = {
flatexhibitors : [],
datafairs : [],
getAllExhibitors : function (wop) {
var deferred = $q.defer();
var searchindex = wop;
var url = '../register/backend/databaseconnect/getexhibitors.php';
var config = {
params: {
search: searchindex
},
cache:true
};
$http.get(url, config).then(function (data) {
service.datafairs = data.data.rows;
for (var i in service.datafairs) {
service.flatexhibitors.push(service.datafairs[i].doc);
};
deferred.resolve(service.flatexhibitors);
}, function (error) {
console.log(error);
deferred.reject(error);
});
return deferred.promise;
}
}
return service;
})