这是一个表格:
<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>
触发点击事件时,我在 Controller 中调用 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 绑定(bind)。由于我们使用 ng-model
,我们可以从 $scope
获取属性。但是,为名称和注释属性提供默认值可能是个好主意。
当我们调用addMessage
时,我们需要通过调用$child
转到子位置来获取消息。然后我们可以通过调用$add
来添加一条新消息。
<强> Plunker Demo
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();
}
}]);
关于javascript - 使用 angularjs 在 firebase 中存储评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24748313/