简体   繁体   English

使用angularjs在Firebase中存储注释

[英]Storing Comments in firebase using angularjs

Here is a form: 这是一种形式:

<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>

On firing click event, I call the addMessage() function in the controller: 在触发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();
    }
}]);

You're really close, just a couple of things. 您真的很亲近,只有几件事情。

We need to create an AngularFire binding by passing in a Firebase reference. 我们需要通过传入Firebase引用来创建AngularFire绑定。 Since we're using ng-model we can grab the properties from $scope . 由于我们使用的是ng-model ,因此可以从$scope获取属性。 However, it's probably a good idea to provide a default value for the name and comment properties. 但是,最好为name和comment属性提供默认值。

When we call addMessage we need to go to a child location for messages by calling $child . 当我们调用addMessage我们需要通过调用$child转到子位置以获取消息。 Then we can add a new message by calling $add . 然后,我们可以通过调用$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();
    }
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM