简体   繁体   English

如何使用AngularFire将项目添加到列表中某个项目内的特定词典中?

[英]How to add items to a specific dictionary within an item in a list with AngularFire?

The below code shows a list from firebase and shows a corresponding comment field for each item in the list. 以下代码显示了来自Firebase的列表,并显示了列表中每个项目的相应注释字段。 The user can make a comment on that item and it will update the comment field for that item in the list. 用户可以对该项目发表评论,它将更新列表中该项目的评论字段。 Currently, each time a comment is made, it overwrites the previous one, but I'd like for all comments to be saved. 目前,每次发表评论时,都会覆盖前一个评论,但我希望保存所有评论。

How do I make it so that every time a comment is added, the previous ones are saved as well? 我该如何做,以便每次添加评论时,也会保存以前的评论?

http://jsfiddle.net/chrisguzman/PS9J2/ http://jsfiddle.net/chrisguzman/PS9J2/

indx.html indx.html

<div ng-app="MyApp" ng-controller="MyCtrl">
    <div ng-repeat="(id,item) in data">
         <h2>{{item.title}}</h2>

        <input ng-model="item.comment"></input>
        <button type="submit" ng-click="CommentAdd(id)">Comment</button>
    </div>
</div>

app.js app.js

angular.module('MyApp', ['firebase'])
    .controller('MyCtrl',

function MyCtrl($scope, $firebase) {
    var furl = "https://helloworldtest.firebaseio.com";
    var ref = new Firebase(furl);
    $scope.data = $firebase(ref);

    $scope.CommentAdd = function (id) {
        $scope.data.$save(id);
    };

});

The following is the data structure within firebase that is generated {helloworldtest: {-JSQhsAnY5zhf0oVKfbb: {title: "nameA", comment:"Second Comment"}, -JSQhsAnY5zhf0oVKfbb: {title: "nameB", comment:"Second Comment"}} } 以下是在Firebase中生成的数据结构{helloworldtest:{-JSQhsAnY5zhf0oVKfbb:{title:“ nameA”,comment:“ Second Comment”},-JSQhsAnY5zhf0oVKfbb:{title:“ nameB”,comment:“ Second Comment”} }}

However, I'd like to create the following where there is a 'comments' branch that holds all comments. 但是,我想在包含所有评论的“评论”分支中创建以下内容。

{helloworldtest: 
{-JSQhsAnY5zhf0oVKfbb: {title: "nameA", comments:{-JSQhsAnY5zhf0oVKfbb:{Comment:"Second Comment"},-JSQhsAnY5zhf0oVKfbb:{Comment:"First Comment"}}},
{-JSQhsAnYdfdfdffbb: {title: "nameA", comments:{-JSQhsAnY5zhf0oVKfAb:{Comment:"Another Comment"},-JSQhsAnY5zhf0oVKfbb:{Comment:"First Comment"}}}
}

I've tried to do this by replacing 我试图通过替换来做到这一点

$scope.data.$save(id);

with

$scope.data.$add(id);

I've also tried using : 我也尝试过使用:

$scope.data[id].$add({foo: "bar"})

You are saving the comment into a field called comment. 您正在将评论保存到名为评论的字段中。 Instead, use a list called comments and utilize push or $add . 而是使用一个称为注释的列表,并使用push$add

<div ng-app="MyApp" ng-controller="MyCtrl">
    <div ng-repeat="(id,item) in data">
         <h2>{{item.title}}</h2>

        <input ng-model="newComment"></input>
        <button type="submit" ng-click="addComment(id, newComment)">Comment</button>
    </div>
</div>

function MyCtrl($scope, $firebase) {
    var furl = "https://helloworldtest.firebaseio.com";
    var ref = new Firebase(furl+'/items');
    $scope.data = $firebase(ref);

    var $comments = $firebase( commentsRef );

    $scope.addComment = function (id, newComment) {
       ref.child(id).child('comments').push(newComment);
    };

});

Also Don't nest data just because you can . 另外, 不要仅仅因为可以嵌套数据 Instead, consider putting comments in their own path, items in their own path. 相反,请考虑将注释放在自己的路径中,将项目放在自己的路径中。

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

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