我正在使用最新的 Angular.js,1.1.5,它通过资源调用返回承诺。当您有多个请求之后将有另一个依赖于这些请求的请求时,正确的实现是什么?
$scope.save = function() {
var newids = [];
angular.forEach ($scope.template.children, function (value){
//saves the children as a new template and returns the ID
Template.$addNew(value).then(
function( value ){newids.push(value);},
function ( error ) {console.log ('error')}
)
});
//updates the root template
$scope.template.childrenIDs = newids;
Template.$update($scope.template).then(
function( value ){ console.log('successfully saved')},
function ( error ) {console.log ('error')}
)
}
为此,我收到一个错误:
TypeError: Object # has no method 'then'
模板是以下工厂返回资源:
mongoAPI.
factory('Template', ['$resource', '$http', 'CONSTANTS', 'security', function($resource, $http, CONSTANTS, security) {
var actions = {
'addNew': {method:'POST'},
}
var res = $resource(CONSTANTS.url, {}, actions)
return res;
}]);