Kev*_*att 2 javascript jquery angularjs
在我的代码中,我想只打印 数组中的第一个单词,而我正在使用ng-if条件.
HTML:
<div ng-controller="MyCtrl">
<div ng-repeat="Word in Words">
<div ng-if="$index === 0" class="preview">{{Word}}- {{$index}}</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
控制器:
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.Words = ['Mon','Tue','Wed'];
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到我使用ng-if="$index === 0"然后打印div值但它想要工作,不知道为什么?
ngIf来自角度verison 1.2.1,但你使用1.0.3 所以你的ng-if不起作用.如果您将角度版本升级到最新版本,则可以正常工作.
否则我建议使用ngShow指令
使用ng-show在这种情况下,只需使用$first数组中的表演第一个元素
<div ng-show="$first" class="preview">{{Word}}- {{$index}}</div>
Run Code Online (Sandbox Code Playgroud)
当我们使用ngIf和ngShow时:
所述ngIf指令删除或重新创建基于一个{表达式}一个DOM树的一部分
所述ngShow指令显示或隐藏基于提供给所述ngShow属性表达给定的HTML元素