• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

ng-如果不比较ng-repeat的$ index

用户头像
it1352
帮助1

问题说明

在我的代码中,我只想从数组中打印第一个单词,而我正在使用 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/

正确答案

#1

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhceafag
系列文章
更多 icon
同类精品
更多 icon
继续加载