學習筆記-AngularJs(三)

來源:互聯網
上載者:User

標籤:

學習筆記-AngularJs(二)寫了個所有程式語言入門時都必須要寫的Hello World,那麼從現在開始做那個之前說過的互連網大佬介紹的學習例子,當然這裡開始會慢慢按照之前說過的目錄來搭建這個學習的demo,將控制器、過濾器、指令、服務、基本配置都獨立成一個個js檔案,直接貼張:

(有點簡陋,之後再把樣式寫好看些!)

 

這裡實現的功能是這樣的,在前台遍曆phones的對象數組,然後可以按照年齡和名字排序,也可以通過輸入字串過濾檢索。代碼如下:

html:

<!doctype html>
<html ng-app ng-controller="PhoneListCtrl">
<head>
<meta charset=‘utf8‘ />
<title ng-bind="‘Google Phone Gallery:‘ + query"></title>
<!-- <title ng-bind-template="Google Phone Gallery:{{query}}"></title> -->
<script src="http://code.angularjs.org/angular-1.0.1.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div class="example2">
Select:
<select name="select" id="select" ng-model=‘order‘>
<option value="name">名字</option>
<option value="age">年齡</option>
</select>
Search: <input name="search" type="text" ng-model=‘query‘ />
<ul>
<!--迭代器-->
<li ng-repeat=‘phone in phones | filter:query | orderBy:order‘>
{{phone.name}}
<p>{{phone.number}}</p>
</li>
</ul>
</div>
</body>
</html>

js:

function PhoneListCtrl($scope){
$scope.phones = [
{‘name‘:‘xioabin‘,‘number‘:‘18824863682‘,‘age‘:‘12‘},
{‘name‘:‘xioalong‘,‘number‘:‘18824863683‘,‘age‘:‘19‘},
{‘name‘:‘xiaohua‘,‘number‘:‘18824863684‘,‘age‘:‘5‘},
{‘name‘:‘xiaoMING‘,‘number‘:‘18824863685‘,‘age‘:‘1‘},
{‘name‘:‘xiaoTU‘,‘number‘:‘18824863686‘,‘age‘:‘2‘},
{‘name‘:‘xiaoKE‘,‘number‘:‘18824863687‘,‘age‘:‘40‘},
];
$scope.order = ‘name‘;

}

代碼不多,但是也有挺多知識點在裡面的,首先我們使用no-repeat遍曆js檔案裡面的phones對象數組,於是後面在html中出現了ng-repeat=‘phone in phones | filter:query | orderBy:order‘,那這是這樣解釋的,遍曆phones,按照query過濾,order排序,而filter和orderBy則是angularJs的迭代器,相應的內建迭代器還有:currency(貨幣轉換)、json(json格式轉換)、date(日期轉換)、lowercase、uppercase(大小寫轉換)等。而query和order是input中ng-model繫結資料,那這樣就可以即時拿著條件檢索資料。這裡有幾點是要講的:

ng-app    標識以下內容就歸angularjs管理

ng-controller 指得是包裹的內容中是在控制器PhoneListCtrl的控制下,在其範圍下去操作變數和函數

<title ng-bind="‘Google Phone Gallery:‘ + query"></title> 
<title ng-bind-template="Google Phone Gallery:{{query}}"></title>

這裡是兩種資料繫結的形式,ng-bind和ng-bind-template,異同上面已經很明顯地體現出來了!當然還有其他的用途,就是有時候我們是這樣寫的<span>{{bind}}</span>的,然後在拚命重新整理頁面的時候,會經常看到{{bind}}的閃現,那麼用ng-bind和ng-bind-template就可以解決了,像這樣<span ng-bind="bind"></span>。

學習筆記-AngularJs(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.