MEAN Stack:如何将函数的结果更新到数据库?

nj2237

我有一个评论系统,每当单击upvote图标时,都需要将upvotes存储在数据库中。我具有的功能会增加数量,但刷新后会再次显示0。如何将其直接存储到数据库?这是代码:

public / javascripts目录中ang.js

var app=angular.module('peopleComments',['ui.router']);
app.factory('comments',['$http', function($http){
var c={
comments:[]
};

//loading all existing comments with getAll()
c.getAll=function(){
return $http.get('/comments').success(function(data){
    angular.copy(data, c.comments);
});
};

//function which creates the new comments for updating in the database
c.create = function(comment) {
return $http.post('/comments', comment).success(function(data){
c.comments.push(data);
});};
return c;
}]);

app.controller('Base',[
'$scope','comments',function($scope,comments){
  $scope.comments=comments.comments;

    $scope.addComment=function(){
        if(!$scope.username||$scope.username==''){$scope.username='Anonymous';}
        if(!$scope.contents||$scope.contents==''){return;}
        comments.create({
            username: $scope.username,
            contents: $scope.contents,
            });
        $scope.username='';
        $scope.contents='';
    }
$scope.increaseUpvotes=function(comment){   //function which updates the upvotes
  comment.upvotes+=1;
}

}]);
莱克斯

您需要c.update(comment)在服务中创建一个方法,方法接受注释以进行更新,然后$http.put()在单击upvote按钮时在API中调用相应的方法来更新注释。

更新:另一个潜在的问题-如果您不是专门创建一个comment.upvotes属性并将其默认设置为0,那么您comment.upvotes += 1可能是在添加一个,null或者undefined不是真正添加一个。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

laravel如何将字段更新到数据库

来自分类Dev

如何将信息更新到数据库中?

来自分类Dev

如何检查 MEAN 堆栈中的数据库集合

来自分类Dev

每当我更新gridview时如何将今天的日期更新到数据库

来自分类Dev

如何将数据从本地SQL Server数据库更新到联机SQL Server数据库?

来自分类Dev

如何将已编辑的JTable单元格的记录更新到数据库中

来自分类Dev

Webapp with MEAN stack and Java

来自分类Dev

Thinkster MEAN Stack工厂

来自分类Dev

如何更新到数据库

来自分类Dev

Mean.js angularjs查询mongodb数据库

来自分类Dev

Mean.js angularjs查询mongodb数据库

来自分类Dev

如何将表从一个数据库更新到另一个?

来自分类Dev

将刮取的图像的位置保存到数据库-节点/ MEAN

来自分类Dev

在MEAN中,我无法将列表从数据库mongodb中显示到angularjs中

来自分类Dev

显示来自 MongoDB (MEAN Stack) 的数据

来自分类Dev

学习MEAN Stack的最佳资源

来自分类Dev

将 MS-SQL 查询的结果更新到 MySql 数据库

来自分类Dev

为什么mean()和mean(aggregate())返回不同的结果?

来自分类Dev

为什么mean()和mean(aggregate())返回不同的结果?

来自分类Dev

我如何开始使用MEAN Stack?

来自分类Dev

如何将搜索结果与数据库集成

来自分类Dev

无法将数据更新到数据库

来自分类Dev

使用php将数据从表单更新到数据库

来自分类Dev

如何将MEAN应用程序与Ionic合并?

来自分类Dev

如何使用django将数据更新到sql数据库中的表中

来自分类Dev

如何将“ weighted.mean”应用于此数据集?

来自分类Dev

numpy mean函数的参数“ a”如何工作?

来自分类Dev

将记录从ListVIEW更新到数据库

来自分类Dev

无法将记录插入或更新到数据库jsp

Related 相关文章

热门标签

归档