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?
正确答案
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
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20