[英]Storing Comments in firebase using angularjs
这是一种形式:
<span class="input-label">Name</span>
<input ng-model="name" type="text">
<span class="input-label">Comment</span>
<input ng-model="msg" type="text">
<button ng-click="addMessage()">Comment</button>
在触发click事件时,我在控制器中调用addMessage()函数:
app.controller('CommentCtrl', ['$scope', '$firebase', function($scope, $firebase){
var myRootRef = new Firebase("my://app/link");
$scope.addMessage = function() {
/*
* How do I store the name and comment obtained
* from the form in firebase.
*
* set, push does not work at all
*/
$scope.modal.hide();
}
}]);
您真的很亲近,只有几件事情。
我们需要通过传入Firebase引用来创建AngularFire绑定。 由于我们使用的是ng-model
,因此可以从$scope
获取属性。 但是,最好为name和comment属性提供默认值。
当我们调用addMessage
我们需要通过调用$child
转到子位置以获取消息。 然后,我们可以通过调用$add
新消息。
app.controller('CommentCtrl', ['$scope', '$firebase', function($scope, $firebase){
var myRootRef = new Firebase("my://app/link");
$scope.messages = $firebase(myRootRef); // AngularFire binding created here
// default values
$scope.name = '';
$scope.msg= '';
$scope.addMessage = function() {
// go to the messages location and push the item by calling $add
$scope.messages.$child('messages').$add({
name: $scope.name,
comment: $scope.msg
});
// clear out the values after adding them to Firebase
$scope.msg= '';
$scope.name = '';
$scope.modal.hide();
}
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.