forked from travis4all/baucis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular-example-resource.html
38 lines (35 loc) · 1.34 KB
/
angular-example-resource.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<html ng-app="angularexample">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular-resource.min.js"></script>
<script type="text/javascript">
var angularExample =angular.module('angularexample',["ngResource"]);
angularExample.controller("MainCtrl",["$resource","$scope",function($resource,$scope){
var vegetableCollection = $resource("/api/vegetables/");
var vegetableResource = $resource("/api/vegetables/:_id/",{_id:'@_id'},{update: { method: 'PUT' }});
$scope.vegetables = vegetableCollection.query(function(){});
$scope.save = function(vegetable){
vegetableResource.update({_id:vegetable._id},vegetable)
}
$scope.add = function() {
var newResource = new vegetableResource($scope.newVegetable);
newResource.$save(function(){
$scope.vegetables.push(newResource);
});
}
}])
</script>
</head>
<body>
<div ng-controller="MainCtrl">
<div ng-repeat="vegetable in vegetables">
name is: {{vegetable.name}}.<br/>
change name:<input type="text" ng-model="vegetable.name"/><button type="submit" ng-click="save(vegetable)">update</button>
</div>
<div>
add new: <br/>
<input type="text" ng-model="newVegetable.name"/><button type="submit" ng-click="add()">add</button>
</div>
</div>
</body>
</html>