Ale*_*u R 1 angularjs angular-resource q
我正在使用最新的Angular.js,1.1.5,它通过资源调用返回promise.当您有多个请求后,另一个请求依赖于这些请求时,什么是正确的实现?
$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')}
)
}
Run Code Online (Sandbox Code Playgroud)
为此,我收到一个错误:
TypeError:对象#没有方法'然后'
模板是以下工厂返回资源:
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;
}]);
Run Code Online (Sandbox Code Playgroud)
从1.2.0起,$resource暴露出一个$promise:
Template.$addNew(value).$promise.then(
function( value ){newids.push(value);},
function ( error ) {console.log ('error')}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5984 次 |
| 最近记录: |