0

services.js:

mPortalServices.factory('ChannelTypeService', ['$filter', '$http', '$q', function (filter, $http, $q) {
    var ChannelTypeService = {};
    ChannelTypeService.getAll = function () {
        var defered = $q.defer();
        $http.get('jsondata/ChannelType.json').then(function(response){
          defered.resolve(response.data);
        });
        return defered.promise;
    }
    ChannelTypeService.getSingle2 = function (typeId) {
        var defered = $q.defer();
        ChannelTypeService.getAll().then(function(items){
            var filtered = filter('filter')(items, {
                'TypeId': typeId
            });
            defered.resolve(filtered);
        });
        return defered.promise;
    }

    return ChannelTypeService;
}]);

controllers.js:

//some code here...
var firstChannel = channels[0];

ChannelTypeService.getSingle2(firstChannel.ChannelType).then(
    function(activities) {
        $scope.channelType = activities;
        console.log('1111');
        console.log($scope.channelType);
    } , 
    function(reason) {  }
);
console.log("2222");
console.log($scope.channelType);
if ($scope.channelType.Type == 1 ) {
    $location.path("/list1/");
}
else {
    $location.path("/list2/");
}
return;

i want wait result of getSingle2 function, but the code above is asynchronous, how to resolve the question?

Mik378
  • 21,881
  • 15
  • 82
  • 180
Net205
  • 161
  • 1
  • 6
  • 16
  • In which way is your code not working? – Wottensprels Apr 08 '14 at 13:50
  • first, the code print 1111 and undefined , and then print 2222 and object, i want the value of channelType, and used it in if statement behind – Net205 Apr 08 '14 at 14:01
  • It seems that there is no native solution. You can read this [answer][1] [1]: http://stackoverflow.com/questions/13088153/how-to-http-synchronous-call-with-angularjs – Aray Karjauv Apr 08 '14 at 15:55

1 Answers1

0

Change controllers.js to the following:

function someFunction() {
  //some code here...
  var deferred = $q.defer();

  var firstChannel = channels[0];

  ChannelTypeService.getSingle2(firstChannel.ChannelType).then(
      function(activities) {
          $scope.channelType = activities;
          console.log('1111');
          console.log($scope.channelType);

          console.log("2222");
          console.log($scope.channelType);

          // the following code will now work:
          deferred.resolve();
          if ($scope.channelType.Type == 1 ) {
              $location.path("/list1/");
          }
          else {
              $location.path("/list2/");
          }

      } , 
      function(reason) {  }
  );

  return deferred.promise;
}

Any code that needs the result of the above function will have to do:

someFunction.then(function() {
   // guaranteed to run after, for example, $scope.channelType has been set
})

Like Net205 said, if you want to force getSingle2 to be synchronous, you in general cannot do that.

Robert Balicki
  • 1,583
  • 2
  • 16
  • 24