从局部变量填充ngResource列表?

时间:2016-11-19 16:53:03

标签: angularjs ngresource

我有一个基本的ngResource定义:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mines">
	<tr>
	</tr>
</table>
<button type="button" onclick="shuffle();">Start The game!</button>

在我的Controller中,我希望能够从本地变量而不是传统的 angular.module('factories', []).factory('SeedSource', [ '$resource', function($resource) { return $resource('/seed-sources/:id/', { id: '@id' }, { update: { method: 'PUT' } }); } ]); 方法填充此资源的列表,例如:

.query()

直到我需要调用ngResource方法,例如:

 $scope.seed_sources = json_array_of_seed_sources;

我知道我可以走很长的路,只需将每个项目单独添加到 seed_source.$save() 作为$scope.seed_sources,但我希望有更简洁的方法来实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

如果要保存一组资源,请使用类方法:

for (let i=0; i<seed_sources.length; i++) {
    SeedSource.save(seed_sources[i], function(source) {
        angular.copy(source, seed_sources[i]);
    });
};