angular.js - angularjs $q promise synchronous parameter passing dependency problem
我想大声告诉你
我想大声告诉你 2017-05-15 16:59:02
0
3
1005
app.factory('documentListFactory',['$http','$q',function($http,$q){
        //申明一个延迟
        var deferred = $q.defer();
        $http.get('http://3.cs/index.php?_json=1&_content=1').success(function(response){
            deferred.resolve(response);
        }).error(function(response){
            deferred.reject(response);
        });
        // 返回承诺,这里并不是最终数据,而是访问最终数据的API 
        return deferred.promise;   
    }]);

app.factory('documentTagsFactory',['$http','$q',function($http,$q){
        //申明一个延迟
        return {
            query(ids)
            {
                var deferred = $q.defer();
                $http.post('http://3.cs/index.php/tags/assoc-tags',{
                    'id':ids ? ids : [1,23],
                    'model':'Document'
                }).success(function(response){
                    deferred.resolve(response);
                }).error(function(response){
                    deferred.reject(response);
                });
                return deferred.promise;
            }
        }
    }]);

app.controller('listController',['$scope','$http','$q','documentListFactory','documentTagsFactory',function($scope,$http,$q,documentListFactory,documentTagsFactory){
        
        var documentList = {};
        
        /*
            code.....
        */
        //var promise = $q.all([documentListFactory,documentTagsFactory.query(documentListFactory)]);
        
        promise.then(function(data){
        //console.log(data);
            //console.log(documentList);
            //console.log(3);
        });
        
        
    }]);

As shown in the above code, I use deferred delay for two facotry, and use promise to perform similar synchronous loading.
But now there is a problem
The method query(ids) in my documentTagsFactory requires a parameters are passed, and this parameter depends on Relying on the result of documentListFactory,
I am new to angularjs, but I don’t know how to implement it here. I use $q.all to achieve multiple synchronizations, but I still can’t pass parameters. I wonder if you have any methods,
PS: I really don’t like

similar to jquery
$.get(function(){
    $.get(function(){
    })
})

or

promise.then(function(){
    promise2.then(function(){
    })
})

This kind of nesting.
What I want is a synchronization-like and non-nested method, thank you

我想大声告诉你
我想大声告诉你

reply all(3)
过去多啦不再A梦

http://stackoverflow.com/questions/24402911/abort-angularjs-http-request-deeply-nested-in-multiple-service-calls

大家讲道理

The result returned by $http execution is a promise. What does it mean if you encapsulate it again?

app.factory('documentListFactory',['$http','$q',function($http,$q){
    //申明一个延迟
    var deferred = $q.defer();
    $http.get('http://3.cs/index.php?_json=1&_content=1').success(function(response){
        deferred.resolve(response);
    }).error(function(response){
        deferred.reject(response);
    });
    // 返回承诺,这里并不是最终数据,而是访问最终数据的API 
    return deferred.promise;
}]);

Wouldn’t it be over if I just write it like this

app.factory('documentListFactory',['$http',function($http){
    return $http.get('http://3.cs/index.php?_json=1&_content=1');
}]);

app.factory('documentTagsFactory', ['$http', function ($http) {
    return {
        query: function (ids) {
            return $http.post('http://3.cs/index.php/tags/assoc-tags', {
                'id': ids ? ids : [1, 23],
                'model': 'Document'
            });
        }
    }
}]);

I can understand avoiding callback hell, but the usage of promise is nested, so you have to write it like this even if you don’t like it! If you want to write synchronization, use ES6’s Generetor or ES7’s async

documentListFactory
        .success(function (ids) {
            return documentTagsFactory.query(ids);
        })
        .success(function (data) {                
            //todo
        });
淡淡烟草味

Use $q.when method

(function () {
    var app = angular.module("myApp", []);
    app.service('noPromise', function() {
        return {
            getResult: function() {
                return { status: "noPromise" };
            }
        };
    });
    app.service("promise", ['$q', '$timeout', function($q, $timeout) {
        return {
            getResult: function() {
                var deferral = $q.defer();
                $timeout(function() {
                    deferral.resolve( { status: "promise" } );
                }, 1000);
                return deferral.promise;
            }
        };
    }]);
    app.run(['$rootScope', '$q', 'noPromise', 'promise', 
             function ($rootScope, $q, noPromise, promise) {
                $rootScope.status = 'Ready.'; 
                 $q.when(noPromise.getResult()).then(function(result) {
                     $rootScope.status = result.status;
                 });
                 $q.when(promise.getResult()).then(function(result) {
                     $rootScope.status = result.status;
                 });
    }]);
})();

http://jsfiddle.net/hjzheng/nd1wfkj3/

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template