Angular post到nodejs
我只想发布一些更改到数据库。 这是我的angular度 – 问题是,它不会从我的HTMLinput值。
参见代码第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
在你的ng-repeat
指令中, post
是当前的post $scope.post
。 只要通过您正在处理的文章到您的编辑function:
<span class="submitcontent" ng-click="editTodo(post)">Submit</span>
JavaScript的:
$scope.editTodo = function(p) { console.log(p); // will have your id, title and content. };