javascript - 使用 angularjs 在 firebase 中存储评论

标签 javascript database angularjs firebase angularfire

这是一个表格:

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

相关文章:

javascript - 长度不定的跨度元素

javascript - jQuery 中的状态管理是什么?

mysql - 根据其他两列之间的较大值选择列

sql - 列出连接表记录

angularjs - AngularFire 中 $getAuth 和 $onAuth 之间的区别

javascript - Payload js解密

javascript - 如何将1个 Angular Controller 分成几个文件?

javascript - 使用javascript对表的所有列进行排序的通用函数

javascript - 为什么计算的模/余数运算符对于大数是错误的?

php - 存储求职者信息的数据库设计