SOLUTION: var deferred2 = $q.defer();
It is important to place this inside the method _getSpecificPerson(), as visiting other Person promises in already resolved. This ensures that a new deferred object is created each time.
Imagine having a list of persons and visiting a details page with a unique URL like: #/PersonDetails/0c4274ed-ae76-4648-bba4-434a3040b9c5. However, upon returning to view details of another person, the same page is shown even after sending a new request for the other person's data. Angular seems to ignore new data if it was previously populated.
Relevant code snippet:
personModule.controller("PersonDetailsController", function ($scope, $http, dataService, $routeParams) {
var id = $routeParams.PersonId;
var myPromise = dataService.getSpecificPerson(id);
myPromise.then(function (result) {
var returnedPerson = result;
if (returnedPerson.AllXml) {
$scope.OnePerson = JSON.parse(returnedPerson.AllXml);
}
});
});
personModule.factory('dataService', function ($http,$q) {
var deferred = $q.defer();
var deferred2 = $q.defer();
var _getPersons= function() {
$http.post(ROOT + 'Home/GetPersons').
then(function(data, status, headers, config) {
deferred.resolve(data.data);
},function () {
deferred.reject();
});
return deferred.promise;
}
var _getSpecificPerson = function (id) {
var dataString = {
id:id
}
$http.post(ROOT + 'Home/GetPerson', dataString).
then(function (data, status, headers, config) {
deferred2.resolve(data.data);
}, function () {
deferred2.reject();
});
return deferred2.promise;
}
return {
getPersons: _getPersons,
getSpecificPerson:_getSpecificPerson
}
}); I attempted to assign my scope within $scope.$apply() but encountered the error: $digest already in progress. Note that the factory function returns correct data from the service, but when using myPromise.then(function(result){ in the controller, the result is cached. However, when logging data.data inside the factory method, _getSpecificPerson returns up-to-date data.