简体   繁体   English

Angularjs UI在ngRepeat中崩溃

[英]Angularjs UI collapse in ngRepeat

I am trying to embed some collapsible panels in a ngRepeat. 我试图在ngRepeat中嵌入一些可折叠面板。 This is what I have: 这就是我所拥有的:

<div class="panel panel-default" ng-repeat="element in elements">
 <div class="panel-heading">
  {{element.name}}
  <button value="Collapse" ng-click="element.isCollapsed != element.isCollapsed"></button>
 </div>
 <div class="panel-body" collapse="element.isCollapsed">
  Content
 </div>
</div>

Now, when I click on the button, the collapse doesn't work. 现在,当我点击按钮时,折叠不起作用。 From the documentation I understand that the repeater creates a scope for every element . 从文档中我了解到转发器为每个element创建了一个范围。 And the attribute collapse of the panel-body should get the same scope, right? 并且面板体的属性collapse应该具有相同的范围,对吧? It seems that the scope.$watch in the collapse directive is not working properly. 看来collapse指令中的scope.$watch工作不正常。 Or maybe I'm doing something wrong? 或者也许我做错了什么?

Thanks 谢谢

Please check the updated fiddle: http://jsfiddle.net/nZ9Nx/9/ 请检查更新的小提琴: http//jsfiddle.net/nZ9Nx/9/

I have created the app and injected ui-bootstrap in it to have the collapse working. 我创建了应用程序并在其中注入了ui-bootstrap以使崩溃正常工作。

angular.module('test', ['ui.bootstrap']);

The problem is 问题是

<div class="panel-heading" toggle target="collapseOne">

It's hardcoded in the example. 它在示例中是硬编码的。 Replace with... 用。。。来代替...

<div class="panel-heading" toggle target="collapse{{ $index + 1 }}">

Also update this tag 还要更新此标记

<div id="collapse{{ $index + 1 }}" toggleable active-class="in" exclusion-group="accordion1" default="active" class="panel-collapse collapse">

Here is a full example 这是一个完整的例子

<div ng-repeat="item in awesomeThings">
    <div class="panel panel-default">
        <div class="panel-heading" toggle target="collapse{{ $index + 1 }}">
            <h4 class="panel-title">
                {{ item }}
            </h4>
        </div>
        <div id="collapse{{ $index + 1 }}" toggleable active-class="in" exclusion-group="accordion1" default="active" class="panel-collapse collapse">
            <div class="panel-body">
                <!-- ... -->
            </div>
        </div>
    </div>
</div>

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

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