【问题标题】:ng-repeat not working with bootstrap collapseng-repeat 不适用于引导程序崩溃
【发布时间】:2018-01-26 05:24:07
【问题描述】:

我正在使用 bootstrap 4 卡来制作博客文章,当您单击“查看评论”链接时,它应该会在卡页脚中打开一个折叠的 div,所有 cmets 都将显示在该处。折叠在使用硬编码的 html 甚至使用在折叠 div 之前重复的动态数据 {{blog.title}} 时都能正常工作。但是,如果我尝试将 ng-repeat='comment in blog.cmets' 添加到页脚的卡体中,则查看 cmets 按钮甚至不会打开折叠。我看到this post 并尝试实现 $index 但要么我没有正确执行,要么解决方案对我不起作用,有些我将在尝试修复之前发布我的原始代码。

来自第一个 ng-repeat 的博客对象

{
"_id": {"$oid": "5a6a426145cc5a2414ef06c4"},
"publishDate": {"$date": "2018-01-25T20:47:29.182Z"},
"comments": [
    {
        "_id": {"$oid": "5a6a429145cc5a2414ef06c5"},
        "username": "Rawle Juglal",
        "comment": "My first comment",
        "blog": {"$oid": "5a6a426145cc5a2414ef06c4"}
    },
    {
        "_id": {"$oid": "5a6a42a645cc5a2414ef06c6"},
        "username": "Phoenix Juglal",
        "comment": "A comment for deleting",
        "blog": {"$oid": "5a6a426145cc5a2414ef06c4"}
    }
],
"title": "My Coaching Philosophy",
"body": "This will be the first post in my new blog",
"__v": 2

}

blog.html

<div class='container-fluid screen-bg'>
<div class='row'>
    <div class='col-12 text-center'> ...</div><!--End of col-12-->
</div><!--End of row-->
<div class='row pb-3' ng-repeat='blog in $ctrl.blogs'>
    <div class='col-12'>
        <div class='card'>
            <div class='card-header'>
                <div class='row'>
                    <div class='col-3 col-md-2'>...</div><!--End of col-md-2-->
                    <div class='col-9 col-md-7'>
                        <h3 class='d-sm-none denim'>{{blog.title}}</h3>
                    </div><!--End of col-md-7-->
                    <div class='col-12 col-md-3'>
                        <p class='d-sm-none denim'>{{blog.publishDate | date:'mediumDate'}}</p>
                    </div><!--End of col-md-3-->
                </div><!--End of row-->
            </div><!--End of card-header-->
            <div class='card-body'>...</div><!--End of card body-->
            <div class='card-footer'>
                <div class='row'>
                    <div class='col-12'>
                        <a class='float-right view-link' data-toggle='collapse' href="#commentDiv" role="button" aria-expanded="false" aria-controls="commentDiv"><u><span class='h4'><i class="fa fa-comments" aria-hidden="true"></i> View Comments</span></u></a>
                    </div><!--End of col-12-->
                    <div class='col-12'>
                        <div class="collapse" id="commentDiv">
                          <div class="card card-body" ng-repeat='comment in blog.comments'>
                            Some text
                            <p>{{comment.username}}</p>
                          </div><!--End of card-body-->
                        </div><!--End of commentDiv-->
                    </div><!--End of col-12-->
                </div><!--End of row-->         
            </div><!--End of card footer-->
        </div><!--End of card-->
    </div><!--End of col-12-->
</div><!--End of row-->

【问题讨论】:

    标签: angularjs twitter-bootstrap angularjs-ng-repeat


    【解决方案1】:

    尝试将您拥有的 id (commentDiv) 与 $index(ng-repeat 中的 AngularJS 索引)连接起来。这样您就可以为 Bootstrap 工作生成唯一标识符。像这样(只贴代码的相关部分):

    <div class='row pb-3' ng-repeat='blog in $ctrl.blogs track by $index'>
    
    ...
    
    <div class='col-12'>
         <a class='float-right view-link' data-toggle='collapse' href="#commentDiv{{$index}}" role="button" aria-expanded="false" aria-controls="commentDiv{{$index}}"><u><span class='h4'><i class="fa fa-comments" aria-hidden="true"></i> View Comments</span></u></a>
    </div><!--End of col-12-->
    
    ...
    
    <div class="collapse" id="commentDiv{{$index}}">
         <div class="card card-body" ng-repeat='comment in blog.comments'>
               Some text
              <p>{{comment.username}}</p>
         </div><!--End of card-body-->
     </div><!--End of commentDiv-->
    

    【讨论】:

    • 好的,我检查了三次以确保在进行更改时没有语法错误,但这些更改仍未打开。
    • @RawleJugal 您能否发布一个 JSFiddle 或任何类似内容以重现该问题?我自己做过几次,它总是按预期工作。
    • @RawleJugal,另外,请检查使用 data-target 而不是 href 是否适用于您的情况,请告诉我。
    • 我没有使用过 JSFiddle,但我可以尝试重新创建它。因为我现在要预约,所以必须在今晚晚些时候。但如果有用的话,我也可以将此链接留给 github。github.com/RawleJuglal/2018RawleJuglal/tree/development。如果不是对不起,我没有做过这么多。
    • 对于未来在这里的任何人。这个解决方案是正确的,尽管一旦我有正确的数据要显示,崩溃也可以在没有索引的情况下工作。我在描述中的博客对象不正确。我以为我在数组中有 cmets 对象,结果我没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多