1. 程式人生 > >angular中要註意的指令

angular中要註意的指令

ont http cto div 第一個 run href 作用域 下標

1.ng-repeat

遍歷集合,給每個元素生成模板實例,每個實例的作用域中可以用一些特殊屬性,如下:

1 $index    //遍歷集合的下標
2 $first   //遍歷集合中的第一個對象
3 $last   //遍歷集合中的最後一個對象
4 $middle   //遍歷集合第一個和最後一個之間的對象
5 $even     //遍歷集合的偶數對象
6 $odd     //遍歷集合的奇數對象

實例:

1 <ul>
2     <li ng-repeat="char in 
3     [{‘alphabet‘: ‘K‘},
4     {‘alphabet‘: ‘A‘},
5     {‘alphabet‘: ‘V‘},
6 {‘alphabet‘: ‘L‘}, 7 {‘alphabet‘: ‘E‘}, 8 {‘alphabet‘: ‘Z‘}] " ng-show="$even">{{char.alphabet}}</li> 9 </ul>

2.ng-href

href和ng-href的區別在於,ng-href默認表達式生效前不要加載該資源。

看下面的例子:

 1 <ul ng-init="myHref=‘‘">
 2     <li><a ng-href="{{ myHref }}">{{linkText}}</a></
li> 3 <li><a href="{{ myHref }}">可以點擊,但不一定是正確的地址</a></li> 4 </ul> 5 .run(function($rootScope, $timeout) { 6 $rootScope.linkText = ‘尚未加載,您無法點擊‘; 7 $timeout(function() { 8 $rootScope.linkText = ‘請點擊‘ 9 $rootScope.myHref = ‘http://google.com‘;
10 }, 2000); 11 })

3.ng-src

大同小異,即表達式生效前不要加載該資源。

1 <img ng-src="{{imgSrc}}"/>
2 .run(function($rootScope, $timeout) {
3     $timeout(function() {
4         $rootScope.imgSrc = ‘https://octodex.github.com/images/daftpunktocat-guy.gif‘;
5     }, 2000);
6 })

angular中要註意的指令