我只是想对数据库发布一些更改。这是我的角色 - 问题是,它不会从我的html输入中获取值。
见代码第2节:
mainController = function ($scope, $http) {
$scope.post = {};
console.log($scope);
console.log($scope.post);
$http.get('/api/todos')
.success(function(data) {
$scope.posts = data.posts;
$scope.datas = data;
// console.log(data);
})
.error(function(data) {
console.log('Error: ' + data);
});
$scope.editTodo = function() {
console.log($scope.post);
$http.post('/post/edit', $scope.post)
.success(function(data) {
$scope.post = {};
console.log($scope.post);
$scope.posts = data.posts;
$scope.datas = data;
})
.error(function(data) {
console.log('Error: ' + data);
});
};
}
第2节:
<div class="comment" ng-repeat="post in posts | filter:search:strict | orderBy:predicate:reverse">
<div class="comment-content">
<form>
<input type="text" class="" ng-model="post.id" />
<input type="text" class="h2" ng-model="post.title" />
<textarea ng-model="post.content" ></textarea>
<span class="submitcontent" ng-click="editTodo()">
Submit
</span>
</form>
</div>
</div>
我不明白为什么我的$scope.post
obj总是等于:Object {}
而我的节点服务器日志显示:
{ id: undefined, postTitle: undefined, postContent: undefined }
POST /post/edit 200 8ms - 2.92kb
答案 0 :(得分:3)
在ng-repeat
指令post
中,当前帖子不 $scope.post
。只需将您正在处理的帖子传递给您的编辑功能:
<span class="submitcontent" ng-click="editTodo(post)">Submit</span>
JavaScript的:
$scope.editTodo = function(p) {
console.log(p); // will have your id, title and content.
};