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

整理AngularJS中的一些常用指令

 更新時(shí)間:2015年06月16日 11:54:56   投稿:goldensun  
這篇文章主要介紹了整理AngularJS中的一些常用指令,包括ng-app、ng-init、ng-model和ng-repeat這四個(gè)指令的講解,需要的朋友可以參考下

 AngularJS指令用于擴(kuò)展HTML。這些都是先從ng- 前綴的特殊屬性。我們將討論以下指令:

  •     ng-app - 該指令啟動(dòng)一個(gè)AngularJS應(yīng)用。
  •     ng-init - 該指令初始化應(yīng)用程序數(shù)據(jù)。
  •     ng-model - 此指令定義的模型,該模型是變量在AngularJS使用。
  •     ng-repeat - 該指令將重復(fù)集合中的每個(gè)項(xiàng)目的HTML元素。

ng-app指令

ng-app 指令啟動(dòng)一個(gè)AngularJS應(yīng)用。它定義根元素。它會(huì)自動(dòng)初始化或啟動(dòng)加載包含AngularJS應(yīng)用程序的Web頁(yè)面的應(yīng)用程序。它也被用來(lái)加載各種AngularJS模塊AngularJS應(yīng)用。在下面的例子中,我們定義默認(rèn)AngularJS應(yīng)用使用div元素的ng-app 屬性。

<div ng-app="">
...
</div>

ng-init 指令

ng-init 指令初始化一個(gè)AngularJS應(yīng)用程序的數(shù)據(jù)。它被用來(lái)把值在應(yīng)用程序中使用的變量。在下面的例子中,我們將初始化countries數(shù)組。使用JSON語(yǔ)法來(lái)定義countries數(shù)組。

<div ng-app="" ng-init="countries=[{locale:'en-US',name:'United States'},
                  {locale:'en-GB',name:'United Kingdom'},
                  {locale:'en-FR',name:'France'}]">
     
...
</div>

ng-model指令

ng-model指令定義在AngularJS應(yīng)用中使用的模型/變量。在下面的例子中,我們定義了一個(gè)名為“name”的模型。

<div ng-app="">
...
<p>Enter your Name: <input type="text" ng-model="name"></p>
</div>

ng-repeat 指令

ng-repeat 指令重復(fù)html元素集合中的每個(gè)項(xiàng)目。在下面的例子中,我們已經(jīng)迭代了數(shù)組countries。

<div ng-app="">
...
  <p>List of Countries with locale:</p>
  <ol>
   <li ng-repeat="country in countries">
     {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}
   </li>
  </ol>
</div>

例子

下面的例子將展示上述所有指令。
testAngularJS.html

<html>
<title>AngularJS Directives</title>
<body>
<h1>Sample Application</h1>
<div ng-app="" ng-init="countries=[{locale:'en-US',name:'United States'},
                  {locale:'en-GB',name:'United Kingdom'},
                  {locale:'en-FR',name:'France'}]">
  <p>Enter your Name: <input type="text" ng-model="name"></p>
  <p>Hello <span ng-bind="name"></span>!</p>
  <p>List of Countries with locale:</p>
  <ol>
   <li ng-repeat="country in countries">
     {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}
   </li>
  </ol>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>

輸出

在Web瀏覽器打開(kāi)textAngularJS.html。輸入姓名并看到以下結(jié)果。

2015616115415280.png (613×372)

相關(guān)文章

最新評(píng)論