【发布时间】:2015-12-17 20:53:11
【问题描述】:
我正在尝试学习 Firebase,发现 MongoDB 和 Firebase 是不同的。 :-)
要制作一个简单的“喜欢”计数器,在我的视图中:
<span>Likes: {{post.likes}}</span>
<form ng-submit="upLike(post)">
<input type="submit" value="Up"></input>
</form>
在 MongoDB 中,此控制器有效:
$scope.upLike = function(post) {
console.log("Liked!");
var likes = post.likes || 0;
post.likes += 1;
$http.put('http://localhost:8080/api/blogDB/' + post._id, post).then(function(response) { // UPDATE
console.log("Upliked.");
}, function(response) {
console.log("Invalid URL");
});
}
$scope.posts 是一个数组。 post 是数组中的一个对象。
MongoDB 从对象 (post) 中提取 _id,然后使用新数据更新集合 (posts) 中的记录。
对于 Firebase,我无法做到 $http.put。相反,我必须将新数据绑定到 $scope,它会绑定到 Firebase。
app.controller('ShowController', ['$scope', '$firebaseArray',
function($scope, $firebaseArray) {
var ref = new Firebase("https://my-firebase.firebaseio.com/");
$scope.posts = $firebaseArray(ref);
$scope.upLike = function(post) {
console.log("Liked!");
var likes = post.likes || 0;
post.likes += 1;
// What goes here instead of $http.put?
}
$scope.post.likes += 1; 不起作用,因为post 不是$scope 上的数组。
$scope.posts.likes += 1; 不起作用,因为我无法确定哪个帖子获得了点赞。
我应该使用ng-click 而不是ng-submit 吗?我可以让视图增加喜欢,但我不知道如何将此数据附加到 $scope。我应该对ng-model 做点什么吗?
【问题讨论】:
标签: angularjs mongodb firebase angularfire