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?