ng-如果不比较ng-repeat中的$ index [英] ng-if not comparing $index in ng-repeat

查看:102
本文介绍了ng-如果不比较ng-repeat中的$ index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我只想从数组中打印第一个单词,而我正在使用 ng-if 条件。

In my code I want to print only first word from the array, and I am using ng-if condition.

HTML:

<div ng-controller="MyCtrl">
  <div ng-repeat="Word in Words">
   <div ng-if="$index === 0" class="preview">{{Word}}- {{$index}}</div>
  </div>
</div>

控制器:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.Words = ['Mon','Tue','Wed'];
}

如你所见,我使用 ng-if = $ index === 0然后打印div值但它想要工作,不知道为什么?

As you can see I use ng-if="$index === 0" then print div value but it want work, don't know why?

示例: http://jsfiddle.net/kevalbhatt18/twvy0j4a/3/

推荐答案

ngIf来自角度版本1.2.1但您使用的是1.0.3 因此您的ng-if不起作用。如果你将角度版本升级到最新版本它可以很有效。

ngIf comes from angular verison 1.2.1 but you used 1.0.3 So your ng-if is not worked. If you upgrade the angular version to latest it works charmly.

否则我建议使用 ngShow 指令

在此上下文中使用 ng-show ,只需使用 $ first 即可显示第一个元素数组

Use ng-show in this context and simply use $first for show first element in the array

 <div ng-show="$first" class="preview">{{Word}}- {{$index}}</div>

小提琴

当我们使用ngIf和ngShow时:

ngIf 指令根据{表达式}删除或重新创建DOM树的一部分

The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}

ngShow 指令根据提供给ngShow属性的表达式显示或隐藏给定的HTML元素

The ngShow directive shows or hides the given HTML element based on the expression provided to the ngShow attribute

这篇关于ng-如果不比较ng-repeat中的$ index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆