欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular.js回顧ng-app和ng-model使用技巧

 更新時間:2016年04月26日 10:43:07   作者:fareise  
這篇文章主要回顧Angular.js中ng-app和ng-model使用技巧,感興趣的小伙伴們可以參考一下

Angular.js中index.html簡單結(jié)構(gòu):

<!doctype html> 
<html ng-app> 
  <head> 
    <script src="http://code.angularjs.org/angular-1.0.1.min.js"></script> 
  </head> 
  <body> 
    Your name: <input type="text" ng-model="yourname" placeholder="World"> 
    <hr> 
    Hello {{yourname || 'World'}}! 
  </body> 
</html> 

ng-app屬性是angular.js的標(biāo)志語句,它標(biāo)記了angular.js的作用域。ng-app可以添加在很多地方,像上面那樣添加到html標(biāo)簽上,說明angular腳本對整個頁面都起作用。也可以在局部添加ng-app屬性,比如在某一個div內(nèi)添加ng-app,則表明接下來的整個div區(qū)域使用angular腳本解析,而其他位置則不適用angular腳本解析。
ng-model表示建立一個數(shù)據(jù)模型。這里在input輸入姓名的輸入框內(nèi),我們把該定義了一個yourname數(shù)據(jù)模型。定義了該模型后,我們可以在下面進(jìn)行調(diào)用,方法是利用{{}}。這樣就完成了數(shù)據(jù)綁定,當(dāng)我們在輸入框內(nèi)輸入內(nèi)容時,會同步到下面的Hello語句塊中。
ng-model定義的數(shù)據(jù)模型不僅可以用于上述場景,還能在許多情況下得到廣泛應(yīng)用。
1、設(shè)置filter,實現(xiàn)搜索功能
在下面的代碼中,我們利用一個簡單的數(shù)據(jù)模型定義+filter就可以完成一個列表搜索功能。(這是中文網(wǎng)上的實例代碼,先不需要管不清楚的部分)

<div class="container-fluid"> 
 <div class="row-fluid"> 
  <div class="span2"> 
   Search: <input ng-model="query"> 
  </div> 
  <div class="span10"> 
   <ul class="phones"> 
    <li ng-repeat="phone in phones | filter:query"> 
     {{phone.name}} 
    <p>{{phone.snippet}}</p> 
    </li> 
   </ul> 
    </div> 
 </div> 
</div> 

 上述代碼中,為搜索框的input標(biāo)簽綁定了數(shù)據(jù)模型query。這樣,用戶輸入的信息會被同步到query數(shù)據(jù)模型中。在下面的li中,使用filter:query就可以實現(xiàn)列表中的數(shù)據(jù)過濾功能,按照用戶的輸入信息進(jìn)行filter過濾。
2、設(shè)置orderBy,實現(xiàn)列表排序功能
在下面的代碼中,與filter同理,使用orderBy為列表添加一個排序功能:

Search: <input ng-model="query"> 
Sort by: 
<select ng-model="orderProp"> 
 <option value="name">Alphabetical</option> 
 <option value="age">Newest</option> 
</select> 
<ul class="phones"> 
 <li ng-repeat="phone in phones | filter:query | orderBy:orderProp"> 
  {{phone.name}} 
  <p>{{phone.snippet}}</p> 
 </li> 
</ul> 

以上就是關(guān)于ng-app和ng-model使用技巧,溫故知新,希望大家從中可以有所收獲。

相關(guān)文章

最新評論