Rumah > Soal Jawab > teks badan
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);
});
}]);
如上代码,有两个Facotry我都是用deferred延迟,使用promise来进行类似于同步加载的,
但现在有个问题
我documentTagsFactory里面的方法query(ids)是需要一个参数进行传递的,而这个参数依赖于documentListFactory的结果,
angularjs新手,这里我却不知道怎样实现了,使用$q.all确实实现的多个同步,但是却还是没办法传递参数,不知大家有什么方法,
PS:本人非常不喜欢类似于jquery里面的
$.get(function(){
$.get(function(){
})
})
或
promise.then(function(){
promise2.then(function(){
})
})
这种嵌套。
要的是类似于同步,并且非嵌套的方法,谢谢
过去多啦不再A梦2017-05-15 17:01:02
http://stackoverflow.com/questions/24402911/abort-angularjs-http-request-deeply-nested-in-multiple-service-calls
大家讲道理2017-05-15 17:01:02
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;
}]);
Bukankah lebih baik jika anda menulisnya seperti ini
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'
});
}
}
}]);
documentListFactory
.success(function (ids) {
return documentTagsFactory.query(ids);
})
.success(function (data) {
//todo
});
淡淡烟草味2017-05-15 17:01:02
Gunakan kaedah $q.when
(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/