[英]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.