【问题标题】:Hide previous content on second click of the button of any loop inside ng repeat在第二次单击 ng repeat 内任何循环的按钮时隐藏以前的内容
【发布时间】:2017-01-20 20:31:58
【问题描述】:

我在ng repeat 中有一个ng-show。每当我单击comment button 时,它都会显示 div 标签包含注释框,其中显示了该列表的先前 cmets。

实际的问题是,当我单击一个评论按钮时,它会显示带有内容的评论框,但是当我单击另一个评论按钮时,会显示带有内容的评论框,但它也会更改前一个评论框的内容(我的意思是showallcomments which is over-writing every time 当我点击评论按钮时)。

注意:用更简单的话the problem is-点击评论按钮,之前点击评论按钮打开的评论框不会关闭

注意:评论框不是不弹出的模型

所以我的解决方案是每当我点击第二个评论按钮时,它应该隐藏所有以前的评论框。(我每次点击评论按钮时都需要隐藏所有之前打开的评论框)

我该怎么做?谁能帮帮我

如果您查看我的代码,我想您可能会理解这个问题。

我的代码是,

            <div class="col-lg-12" ng-repeat="dat in details | orderBy : sortColumn : reverseSort | filter : { product_name : textname} as results">
                    <ul>
                        <li><b>Product:</b><span> {{dat.product_name}}</span></li>
                        <li><b>Product Manager:</b><span> {{dat.first_name}} {{dat.last_name}}</span></li>
                        <li><b>Status:</b><span> {{dat.status}}</span></li>
                        <li><b>Description:</b><span> {{dat.description}}</span></li>
                    </ul>
                    <!--Comment Button -->
                    <button style="background-color:#4C97C8;color:white;height:30px" class="btn" ng-click="comment=true;$parent.showcomments(dat.id)"><span class="glyphicon glyphicon-comment"></span><strong> Comment</strong></button>

                    <!--Comment Box -->
                    <div ng-show="comment">
                     <div class="detailBox col-lg-12">
                        <div class="titleBox">
                            <label>Comment Box</label>
                            <button type="button" class="close" aria-hidden="true" ng-click="comment=false">&times;</button>
                        </div>
                        <div class="actionBox">
                            <ul class="commentList">
                                <li ng-repeat="sh in showallcomments">
                                <div class="commenterImage">
                                    <img src="" />
                                </div>
                                <div class="commentText">
                                    <p class="">{{sh.comment}}</p> <span class="date sub-text">{{sh.date_created}}</span>
                                </div>
                                </li>
                            </ul>
                            <div class="input-group ">
                                <input type="text" id="commentarea" name="commentarea" class="form-control" placeholder="Your Comments" aria-describedby="basic-addon2" ng-model="takecomment">
                                <span class="input-group-addon" id="basic-addon2" ng-click="takecomment=mycomment(dat.id,takecomment)"><span class="glyphicon glyphicon-send"></span></span>
                            </div>
                        </div>
                     </div>
                    </div>
            </div>

【问题讨论】:

    标签: angularjs angularjs-ng-repeat angularjs-ng-click angularjs-ng-show


    【解决方案1】:

    你可以试试:

    <button style="background-color:#4C97C8;color:white;height:30px" class="btn" ng-click="$parent.commentId=dat.id;$parent.showcomments(dat.id)"><span class="glyphicon glyphicon-comment"></span><strong> Comment</strong></button>
    

    <div ng-show="dat.id===$parent.commentId">
    

    【讨论】:

    • 没有工作,你没有注意到我在评论框上方的评论按钮
    • 没有再次工作。单击评论按钮时,先前通过单击评论按钮打开的评论框不会关闭。还有其他想法吗?
    • 在这两个地方都尝试$parent.commentId 而不是commentId
    • ng-click="$parent.commentId=dat.id" 没有括号
    【解决方案2】:
        I think you are showing the content through modal "showallcomments". which is over-writing every time when you click on comment button.
    
        I mean to say you are binding same variable in all comment boxes.
    
    
     <div class="col-lg-12" ng-repeat="dat in details | orderBy : sortColumn : reverseSort | filter : { product_name : textname} as results">
                        <ul>
                            <li><b>Product:</b><span> {{dat.product_name}}</span></li>
                            <li><b>Product Manager:</b><span> {{dat.first_name}} {{dat.last_name}}</span></li>
                            <li><b>Status:</b><span> {{dat.status}}</span></li>
                            <li><b>Description:</b><span> {{dat.description}}</span></li>
                        </ul>
                        <!--Comment Button -->
                        <button style="background-color:#4C97C8;color:white;height:30px" class="btn" ng-click="showCommentBox($index);$parent.showcomments(dat.id)">
                        <span class="glyphicon glyphicon-comment"></span><strong> Comment</strong></button>
    
                        <!--Comment Box -->
                        <div ng-show="dat.showComment">
                         <div class="detailBox col-lg-12">
                            <div class="titleBox">
                                <label>Comment Box</label>
                                <button type="button" class="close" aria-hidden="true" ng-click="comment=false">&times;</button>
                            </div>
                            <div class="actionBox">
                                <ul class="commentList">
                                    <li ng-repeat="sh in showallcomments">
                                    <div class="commenterImage">
                                        <img src="" />
                                    </div>
                                    <div class="commentText">
                                        <p class="">{{sh.comment}}</p> <span class="date sub-text">{{sh.date_created}}</span>
                                    </div>
                                    </li>
                                </ul>
                                <div class="input-group ">
                                    <input type="text" id="commentarea" name="commentarea" class="form-control" placeholder="Your Comments" aria-describedby="basic-addon2" ng-model="takecomment">
                                    <span class="input-group-addon" id="basic-addon2" ng-click="takecomment=mycomment(dat.id,takecomment)"><span class="glyphicon glyphicon-send"></span></span>
                                </div>
                            </div>
                         </div>
                        </div>
                </div>
    
    
                showCommentBox = function(index){
    
                    angular.forEach('details', function(value, key){
    
                        if(key == index){
                            value.showComment = true;
                        }else{
                            value.showComment = false;
                        }
                    })
    
                }
    

    【讨论】:

    • 正确..我的问题不同 - 我每次点击评论按钮时都需要隐藏所有以前打开的评论框。
    • 好的,所以您用于显示评论部分的标志不应该是单一的,应该有多个标志。您可以在详细信息的每个对象中添加属性 showComment 并仅将标记 showComment 设置为单击对象。
    • 我也编辑了代码,我认为它可以帮助你。
    • 不起作用..我除了这个问题的其他答案之外的答案
    猜你喜欢
    • 2015-09-02
    • 2020-02-23
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多