我试图通过保存发布一些数据,但除非我刷新,否则无法在浏览器中看到数据,todo.id正在显示但不是todo.name
<body ng-controller="TodoCtrl">
<div ng-repeat="todo in Todos.record">
<div>{{todo.id}} : {{todo.name }}</div>
</div>
<div>
<input ng-model="todo.name" />
<button ng-click="addItem()">Add Item</button>
</div>
</body>
var app = angular.module("myApp",['ngResource']);
app.factory("Todo",function($resource){
return $resource(
"http://localhost/rest/motors/users?app_name=motors",
{},
{
query: {method: 'GET',isArray: true},
get: {method: 'GET'},
save:{method:'POST'}
}
)
})
app.controller("TodoCtrl", function ($scope, Todo) {
"use strict";
$scope.Todos = Todo.get();
$scope.addItem = function(){
Todo.save($scope.todo, function(data){
$scope.Todos.record.push(data);
$scope.todo={};
});
}
})
答案 0 :(得分:0)
如果该代码示例代表您的实际代码,那么引用“todo.name”的“输入”字段是ng-repeat循环的外部可能是一个问题,因此这是在您的外部定义模型属性“Todos.record”列表。
答案 1 :(得分:0)
在api url中添加&amp; fields = *解决了这个问题。例如改变了
"http://myexample.com/rest/motors/users?app_name=motors"
to
"http://myexample.com/rest/motors/users?app_name=motors&fields=*"
感谢您的帮助。