Angularjs ng-if停止渲染div

时间:2014-01-09 15:38:26

标签: angularjs angular-ng-if

我正在学习AngularJS,我想在div时将$index == 2括在另一个

这是我的代码

<span ng-repeat="element in [0,1,2,3]">
    <div ng-if="$index == 2"> 
        <div> I'm the div
    </div>                  
    <div>
         My element: {{element}} - Index: {{$index}}
    </div>      
    <div ng-if="$index == 2"> 
        </div> I'm the /div
    </div>
</span>  

Fiddlehttp://jsfiddle.net/2prRw/3/

问题

“我的元素:0”,“我的元素:1”和“我的元素:3”永远不会显示。

结果是:

I'm the div
My element: 2 - Index: 2
I'm the /div

谢谢!

修改

预期结果是:

   span
     div
         My element: 0
     /div
     div
         My element: 1
     /div
     div
         div
            My element: 2
         /div
     /div
     div
         My element: 3
     /div
   /span

1 个答案:

答案 0 :(得分:2)

解决方法,但会导致一些重复:

<span ng-repeat="element in [0,1,2,3]">
    <div ng-if="$index == 2"> 
        <div> I'm the div
            <div>
                My element: {{element}} - Index: {{$index}}
            </div>      
        </div> I'm the /div
    </div>
    <div ng-if="$index != 2">
        My element: {{element}} - Index: {{$index}}
    </div>
</span>  

JSFiddle