问题描述
限时送ChatGPT账号..我需要在 AngularJS 1.0.7 中使用参数运行一个函数 "searchBoats(boatType)".此参数是另一个函数 parseBoatType 的结果,该函数运行的服务使用 $resource 调用 API.当资源中带有 promise 的船类型返回时,我如何运行 searchBoats?
I need to run a function "searchBoats(boatType)" in AngularJS 1.0.7 with a parameter. This parameter is the result of another function parseBoatType which is running a service that calls an API with a $resource. How can I run searchBoats when the boatType is returned in the resource with promises?
这是我试过的:
var parseURL = function() {
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function success (result) {
console.log(result);
searchBoats(result);
});
deferred.resolve(
parseBoatType()
);
};
parseURL();
var parseBoatType = function() {
// Do some stuff
// Calculate boatType calling a service that uses resource to call
// an API
// I can convert this callback into a promise but still facing same
// issue
BoatType.getBoatTypeByName({name: boatTypeParsed}, function success(result) {
return result;
});
// The service method is called and the code is still running until
// the end of the function without waiting for the service result.
// Then the promise.then code in the parseURL is executed and
// searchBoats is run with boatType undefined.
};
// The service with the $resource call to the API
.factory('BoatType',
function($resource, SERVER_URL){
var boatTypes =
$resource('http://' + SERVER_URL +'/:action', {action:'boat_types'}, {
query: {method:'GET', isArray: true},
getBoatTypeByName: {method:'GET', params:{action: 'getBoatTypeByName'}, isArray: false}
});
return boatTypes;
}
)
推荐答案
您可以从 parseBoatTime
资源中的 BoatType
资源返回资源 $promise
code> 函数并使用 promise 来解析 parseUrl
deferred.
You can return the resource $promise
from the BoatType
resource in the parseBoatTime
function and use the promise to resolve the parseUrl
deferred.
首先从 parseBoatTime
函数返回一个承诺:
First return a promise from the parseBoatTime
function:
return BoatType.getBoatTypeByName({
name: boatTypeParsed
}, function success(result) {
return result;
}).$promise;
然后使用来自 BoatType
资源的承诺解析 parseUrl
deferred
:
Then resolve the parseUrl
deferred
with the promise from the BoatType
resource:
parseBoatType().then(deferred.resolve);
Bellow 是从您的问题中提取的完整代码,并进行了我提到的更正.
Bellow is the full code taken from your question with the correction I mentioned.
var parseURL = function() {
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function success(result) {
console.log(result);
searchBoats(result);
});
parseBoatType().then(deferred.resolve);
};
parseURL();
var parseBoatType = function() {
// Do some stuff
// Calculate boatType calling a service that uses resource to call
// an API
// I can convert this callback into a promise but still facing same
// issue
// Code for ngResource@^1.2.0
/*return BoatType.getBoatTypeByName({
name: boatTypeParsed
}, function success(result) {
return result;
}).$promise;*/
// Code for ngResource lower than 1.2.0
var deferred = $q.defer(), promise = deferred.promise;
BoatType.getBoatTypeByName({
name: boatTypeParsed
}, deferred.resolve, deferred.reject);
return promise;
// The service method is called and the code is still running until
// the end of the function without waiting for the service result.
// Then the promise.then code in the parseURL is executed and
// searchBoats is run with boatType undefined.
};
// The service with the $resource call to the API
app.factory('BoatType',
function($resource, SERVER_URL) {
var boatTypes =
$resource('http://' + SERVER_URL + '/:action', {
action: 'boat_types'
}, {
query: {
method: 'GET',
isArray: true
},
getBoatTypeByName: {
method: 'GET',
params: {
action: 'getBoatTypeByName'
},
isArray: false
}
});
return boatTypes;
}
)
这篇关于在 AngularJS 1.0.7 中使用 $resources 嵌套 promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论