gpt4 book ai didi

javascript - Angular 发布到 nodejs

转载 作者:行者123 更新时间:2023-11-29 05:22:59 27 4
gpt4 key购买 nike

我只想发布对数据库的一些更改。这是我的 Angular - 问题是,它不从我的 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 {} 而我来自 Node 服务器的日志显示:

{ id: undefined, postTitle: undefined, postContent: undefined }
POST /post/edit 200 8ms - 2.92kb

最佳答案

在您的 ng-repeat 指令中,post 是当前帖子 NOT $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.
};

关于javascript - Angular 发布到 nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23423469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com