【发布时间】:2015-11-24 03:34:20
【问题描述】:
我正在学习 ngResource。
我可以“获取” json 文件的所有值。但我不能使用一个值(使用查询)并更新其他值。
我明白了:
this.total = userService.query();
我正在尝试购买:
this.user = userService.query({"name":"Luis"});
我正在尝试更新:
var userToUpdate = userService.get({},{"name":"Luis"});
userToUpdate.name = "Name changed";
userToUpdate.$save();
现在是通用代码
file1.html
<div ng-app="module" ng-controller="controls as ctrl">
<div ng-repeat="item in ctrl.total">
<span ng-bind="item.name"></span>
</div>
{{ ctrl.total }}
<br /><br />
{{ ctrl.user }}
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-resource.js"></script>
<script>
var x = angular.module("module", ["ngResource"]);
x.factory("userService", ["$resource", function($resource)
{
return $resource("data.json");
}]);
x.controller("controls", ["userService", function(userService)
{
this.total = userService.query();
this.user = userService.query({"name":"Luis"});
var userToUpdate = userService.get({},{"name":"Luis"});
userToUpdate.name = "Name changed";
userToUpdate.$save();
}]);
</script>
data.json
[
{"id": 1, "name": "Mario", "lastname": "Perez"},
{"id": 2, "name": "Raul", "lastname": "Alvarez"},
{"id": 3, "name": "Pedro", "lastname": "Tomas"},
{"id": 4, "name": "Luis", "lastname": "Ranks"}
]
有人可以帮助我吗?我是新手。 感谢您的宝贵时间。
【问题讨论】:
标签: angularjs ngresource