Javascript 存储输入表单AngularJS中的数据
我想存储注释的数据,并将其显示在视图中,然后存储在db中 我有这张表格Javascript 存储输入表单AngularJS中的数据,javascript,angularjs,arrays,forms,object,Javascript,Angularjs,Arrays,Forms,Object,我想存储注释的数据,并将其显示在视图中,然后存储在db中 我有这张表格 <form> <div class="form-group"> <label>Add a comment</label> <input type="text" ng-model="newRole.newAuthor" placeholder="author"> <i
<form>
<div class="form-group">
<label>Add a comment</label>
<input type="text" ng-model="newRole.newAuthor" placeholder="author">
<input type="date" ng-model="newRole.newDate">
<input type="file" ng-model="newRole.commentImage">
<textarea class="form-control metro" ng-model="newRole.newComment"></textarea>
<h2>{{txtcomment}}</h2>
</div>
</form>
<button class="btn btn-primary" ng-click="trip.makeComment(newRole)">Comment</button>
这是控制器:
我知道控制器或表单有问题,但我不知道是什么。没有
$scope.newDate
,等等——是$scope.newRole.newDate
,对吗?尽管如此,由于您将newRole
作为参数传入,您可以这样做:
this.tripObject.comments.push({
author: newRole.newAuthor,
date: newRole.newDate,
imageURL: newRole.commentImage,
text: newRole.newComment
})
(另外,您可能不应该将newRole
重新分配给具有相同名称的新变量)。首先声明
$scope.tripObject={comments:[]}
然后宣布
$scope.newRole={}
然后单击js中的makeComment
$scope.tripObject.comments.push($scope.newRole)
这里newRole是所有项目将推送的对象您调试了吗?是的,我调试了。这很有效!太棒了,但是如果我需要将评论存储在数据库中呢?如何获取之前推送的对象并使用put或post进行存储?与先执行
push
不同,您只需将newRole
分配给另一个变量,然后使用它生成ajax post,如果成功,则在回调中执行push
。
this.makeComment = function(newRole){
// $scope.txtcomment = '';
var newRole = {
"author":newRole.newAuthor,
"date":$scope.newDate,
"imageURL":$scope.commentImage,
"text" : $scope.newComment
}
console.log($scope.newRole);
console.log($scope.tripObject.comentario)
this.tripObject.comments.push($scope.newRole);
console.log(this.tripObject.comments);
};
this.tripObject.comments.push({
author: newRole.newAuthor,
date: newRole.newDate,
imageURL: newRole.commentImage,
text: newRole.newComment
})