AngularJS: одновременное асинхронное выполнение двух сервисов
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | angular.module('MyModule', []).   // controllers   controller('FolderListCtrl', ['$scope', '$q', 'Async', 'Service1', 'Service2', function FolderListCtrl($scope, $q, Async, Service1, Service2) {     $q.       all([Async(Service1.query), Async(Service2.get)]).       then(function (responses) {         $scope.service1Res = responses[0];         $scope.service2Res = responses[1];       });   }]).   factory('Async', ['$q', function Async($q)   {     return function (ResCall, params) {       var d = $q.defer();       if (angular.isFunction(ResCall))       {         ResCall(params, function(response) {           d.resolve(response);         });         return d.promise;       }       throw new Error('wrong invocation with ' + ResCall.toString());     };   }]) | 
Можно было бы сделать дополнительные проверки, но мне они показались излишними.
По мотивам этого ответа: http://stackoverflow.com/a/15300364/801426
Similar Posts
LEAVE A COMMENT
Для отправки комментария вам необходимо авторизоваться.
One Response so far.