0

SOLUTION: var deferred2 = $q.defer(); This should be inside method _getSpecificPerson() because when I visit other Person promise in alredy resolved. This way I create new deferred object each time.

I have some list, for example list of persons. If I visit details page( different view ,different controller) I have url like : #/PersonDetails/0c4274ed-ae76-4648-bba4-434a3040b9c5, but when a return and want to see some other person details I get same page like before althougt I can see I sent already get request for getting other person. It seems angular ignores new data, if it has some data populated before. This is relevant code if needed:

 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 tryed put my scope assigment inside of $scope.$apply() but I get $digest already in progress: $digest already in progress EDIT: Factory function returns good data from service but when I do myPromise.then(function(result){ inside of my controller result is cached! (but when I console.log(data.data) inside factory method it _getSpecific person returns up-to-date data!

3
  • can you show us what the data in returnedPerson looks like? is it JSON? Commented Jul 25, 2014 at 12:34
  • Yes, it is Json...everythig is great if I refresh page... Commented Jul 25, 2014 at 12:35
  • But when I visit personA, and return to list of persons and then visit personB , I send request and get response for person B but $scope.OnePerson dors not update in template. I tried doing $scope.apply but I doesn't help. Commented Jul 25, 2014 at 12:36

1 Answer 1

0

I everything is JSON you should be able to write

$scope.OnePerson = returnedPerson.AllXml;

Also you could simplify your service

var _getSpecificPerson = function (id) {
    var dataString = { id:id };
    return $http.post(ROOT + 'Home/GetPerson', dataString);
}

Because the post method returns a promise. In your controller you must make this change for it to work:

var returnedPerson = result.data;
Sign up to request clarification or add additional context in comments.

7 Comments

Factory function returns good data from service but when I do myPromise.then(function(result){ inside of my controller result is cached!
Are you really sure that $scope.OnePerson gets the new data? If you do a console.log($scope.OnePerson) inside of the if-statement does it show the new data?
No $scope.OnePerson gets old data, server returns new data if I console.log(data.data) a get FRESH data...but inside controller myPromise.then(function(result)........result is OLD.
Here is the problem, my promise function is called before faytory method. How this is possible?
It's difficult to see what's wrong when you can't see all of the code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.