AngularJS實(shí)現(xiàn)分頁顯示數(shù)據(jù)庫信息
接著第一篇《》AngularJS內(nèi)建服務(wù)$location及其功能詳解》,進(jìn)行學(xué)習(xí)
Section 2:實(shí)現(xiàn)分頁顯示效果
那么再隱身一下,通過location的setter方法設(shè)置當(dāng)前的url信息。在這里為了能夠讓演示看到更好的效果,在這個比較完整的實(shí)例中,我引入了angularJS的多路由技術(shù)、嵌套的控制器之間傳遞數(shù)據(jù)、scope的繼承、 http通信、內(nèi)鏈接傳遞變量等。
首先建立一個首頁模板
<!DOCTYPE html>
<html ng-app="turnPageApp">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="lib/angular.js"></script>
<script src="lib/angular-route.min.js"></script>
<style type="text/css">
.turnPageButtonArea {
background-color: #f07a13;
width: 500px;
height: 30px;
line-height: 30px;
text-align: center;
margin: 10px auto;
padding: 20px;
}
button {
margin-right: 20px;
width: 100px;
height: 30px;
font-size: 15px;
}
.pageNum {
width: 50px;
height: 23px;
text-align: center;
}
body {
font-family: 微軟雅黑;
}
h1 {
text-align: center;
}
.totalPages {
color: #ffffff
}
</style>
</head>
<body ng-controller="indexController">
<h1>AngularJS TurnPage By $location Service</h1>
<div ng-view></div>
<div class="turnPageButtonArea">
<button ng-click="previous()">Previous</button>
<button ng-click="next()">Next</button>
<input type="number" ng-model="currentPage" class="pageNum">
<button ng-click="goToPage()">Go</button>
<span class="totalPages">共 {{allPage}} 頁</span>
</div>
</body>
</html>
通過一些簡單的CSS樣式將頁面的布局修飾了一下。
然后在首頁的元素里設(shè)置了一些ngApp和controller。
在屬性為ngView的div元素中,將嵌入其他HTML的模板。
緊接著下方,我擺放了三個按鈕,其中前兩個是上一頁和下一頁的翻頁按鈕;一個輸入框可供用戶輸入他想跳轉(zhuǎn)到的頁碼,旁邊的按鈕作為頁碼的提交按鈕,點(diǎn)擊后頁面立即翻頁。
這三個按鈕里面都有一個ngClick屬性,表示當(dāng)用戶點(diǎn)擊按鈕后,便會執(zhí)行對應(yīng)的函數(shù)。
在講解angularJS的js代碼前,先截圖看看文件的目錄結(jié)構(gòu)。

上面的index.html是之前兩個例子的,可以不去理會。
為了簡單期間,我把script腳本都放在了turnPage.html文件的下方了。下面就是全這個文件的完整代碼:
turnPage.html
<!DOCTYPE html>
<html ng-app="turnPageApp">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="lib/angular.js"></script>
<script src="lib/angular-route.min.js"></script>
<style type="text/css">
.turnPageButtonArea {
background-color: #f07a13;
width: 500px;
height: 30px;
line-height: 30px;
text-align: center;
margin: 10px auto;
padding: 20px;
}
button {
margin-right: 20px;
width: 100px;
height: 30px;
font-size: 15px;
}
.pageNum {
width: 50px;
height: 23px;
text-align: center;
}
body {
font-family: 微軟雅黑;
}
h1 {
text-align: center;
}
.totalPages {
color: #ffffff
}
</style>
</head>
<body ng-controller="indexController">
<h1>AngularJS TurnPage By $location Service</h1>
<div ng-view></div>
<div class="turnPageButtonArea">
<button ng-click="previous()">Previous</button>
<button ng-click="next()">Next</button>
<input type="number" ng-model="currentPage" class="pageNum">
<button ng-click="goToPage()">Go</button>
<span class="totalPages">共 {{allPage}} 頁</span>
</div>
<script>
//實(shí)例化用戶自己的ngApp對象。這里因?yàn)橛玫搅寺酚蓹C(jī)制,所以在依賴注入中寫入ngRoute這個模塊
var turnPageApp = angular.module('turnPageApp', ['ngRoute']);
/*
設(shè)置對于不同的url,啟用不同的模板和控制器。
本例由于只用到了一個模板,所以遇到的路由的情況就只有一種,即 “/id”
*/
turnPageApp.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/:id', { //這里的id其實(shí)表示的是翻頁中的頁碼
templateUrl: 'view/student.html',
controller: 'StudentController'
})
.otherwise({redirectTo: '/1'});//如果沒法匹配到url時,直接跳轉(zhuǎn)會第一頁
}]);
//注冊父控制器indexController,這里由于需要將模板里的子控制器的值傳遞給父控制器,所以需要這個根域$rootScope來幫忙,在返回函數(shù)里需要傳入這個根域?qū)ο蟆?
//而且,本例是基于對url的操作來實(shí)現(xiàn)翻頁,所以這個內(nèi)建的$location服務(wù)是一定要傳入的
turnPageApp.controller('indexController', function ($rootScope, $scope, $location) {
//給父scope定義函數(shù)
$scope.previous = function () {
//從瀏覽器的地址欄獲取路徑,即turnPage.html#/1中井號后面的內(nèi)容:“ /1 ”
//然后通過JavaScript的silce函數(shù)取出斜杠后面的字符,并轉(zhuǎn)換成數(shù)字。
//加 1 還是減 1 要看是在定義的是哪個按鈕的功能函數(shù)了
var pageNum = parseInt($location.path().slice(1)) - 1;
//頁碼是有限的,需要做一些判斷
if (pageNum < 1) {
alert('This is the first page');
} else {
//如果現(xiàn)在沒有處在第一頁,則path屬性減去1,即向前翻一頁。這個翻頁的效果就是通過設(shè)置url中的path來實(shí)現(xiàn)
$location.path(pageNum);
}
};
//和上面的previous()函數(shù)類似
$scope.next = function () {
var pageNum = parseInt($location.path().slice(1)) + 1;
if (pageNum > $rootScope.allPage) {
alert('This is the last page');
} else {
$location.path(pageNum);
}
};
//這是一個直接跳轉(zhuǎn)到那個頁碼的函數(shù),在判斷的地方稍微繁瑣些
$scope.goToPage = function () {
//從input輸入框綁定的currentPage變量中獲取用戶輸入的值
var pageNum = $scope.currentPage;
//為了程序的嚴(yán)密和可用性,需要先判斷輸入是否為空
if (pageNum == null || pageNum == undefined || pageNum == "") {
alert("try to input a page number");
//如果不是空,再判斷用戶輸入的頁碼是否超出了頁碼的范圍
//這里出現(xiàn)了$rootScope這個根域及其屬性allPage,該屬性的值是頁碼的總數(shù)
} else if (!(pageNum >= 1 && pageNum <= $rootScope.allPage)) {
alert("The page number is beyond the scope of the number of the students")
} else {
//如果都沒問題了,則修改URL,此時angularJS會捕捉地址欄的變化,然后跳轉(zhuǎn),細(xì)節(jié)將在下面講解。
$location.path(pageNum);
}
};
});
//這里實(shí)在注冊嵌入到首頁的模板頁的控制器。
//由于子域和父域的通信需要借助根域,所以在依賴中傳入$rootScope對象
//$scope是模板自己的作用域,它繼承了父控制器indexController生成的作用域
//在模板中需要與服務(wù)端的文件進(jìn)行交互,所以需要引入內(nèi)建的$http服務(wù)。為了控制實(shí)例的復(fù)雜度,我直接寫了一個json文件,做了一些假數(shù)據(jù)。
//這里$routeParams是一個對象,這個對象里有一個屬性,就是我們之前在config()函數(shù)里看到的那個id(/:id),這個id是個變量,地址欄里的path是幾,這個id就是幾。id的值需要通過$routeParams這個對象來取得。
turnPageApp.controller('StudentController', ['$rootScope', '$scope', '$http', '$routeParams', function ($rootScope, $scope, $http, $routeParams) {
//$http的get方法與遠(yuǎn)程的一個文件發(fā)出請求,如果成功,則執(zhí)行一個回調(diào)函數(shù),函數(shù)的參數(shù)就是從遠(yuǎn)端文件里拿到的數(shù)據(jù),這個數(shù)據(jù)可以是個數(shù)組,也可以是個對象。
//那么我們這次拿到的是一個json數(shù)組,數(shù)組的元素是一個個的對象。
$http.get('data/students.json').success(function (data) {
//把數(shù)組里的一個元素取出來,賦給模板子作用域?qū)ο蟮膶傩陨?。由于json數(shù)組的id是從1開始寫的,而返回的數(shù)據(jù)是個數(shù)組,下標(biāo)從0開始,所以要減去1
$scope.student = data[$routeParams.id - 1];
//這里順便把這個數(shù)組的元素個數(shù)取出來,每個元素就代表以頁。那么元素總個數(shù)就代表共有多少頁。
//注意要傳遞給最高級別的根域?qū)ο?,因?yàn)樽佑蚰芨矊懜赣虻耐麑傩裕蛔佑蛉绻麤]有直接賦值,那么子域的同名屬性將繼承父域同名屬性的值;
/*我們在回到本文件代碼上面的“共 {{allPage}} 頁”處,這個就是根域$rootScope的屬性。而且在父控制器中并沒有特別的賦值。而這個span元素恰好就在父控制器的作用域內(nèi),那么這個元素里的allPage屬性就會繼承父作用域的同名屬性allPage的值,也就間接的顯示出了總頁數(shù)。
*/
$rootScope.allPage = data.length;
});
}]);
</script>
</body>
</html>
view/student.html
<table cellspacing="0">
<tr>
<td class="title">ID</td>
<td>{{student.id}}</td>
</tr>
<tr>
<td class="title">Name</td>
<td>{{student.name}}</td>
</tr>
<tr>
<td class="title">Sex</td>
<td>{{student.sex}}</td>
</tr>
<tr>
<td class="title">Age</td>
<td>{{student.age}}</td>
</tr>
<tr>
<td class="title">Courses</td>
<td>
<ul>
<li ng-repeat="course in student.courses">{{course}}</li>
</ul>
</td>
</tr>
</table>
<style type="text/css">
table {
border: solid 1px #000000;
margin: 0px auto;
}
td {
padding: 10px 10px 10px 20px;
margin: 0px;
border: solid 1px #000000;
}
tr {
margin: 0px;
padding: 0px;
}
.title {
background-color: #207ef0;
text-align: center;
color: #ffffff;
}
ul {
list-style: none;
margin: 0px;
padding: 0px;
}
li {
float: left;
margin: 10px;
}
</style>
data/students.json
[
{
"id": 1,
"name": "Frank Li",
"sex": "male",
"age": "30",
"courses": [
"HTML",
"CSS",
"Javascript",
"Java",
"PHP",
"MySQL",
"Ubuntu",
"MongoDB",
"NodeJS",
"AngularJS",
"Photoshop",
"LESS",
"Bootstrap"
]
},
{
"id": 2,
"name": "Cherry",
"sex": "female",
"age": "27",
"courses": [
"Anderson's Fairy Tales",
"Pride and Prejudice",
"Vanity Fair",
"Oliver Twist"
]
},
{
"id": 3,
"name": "John Liu",
"sex": "male",
"age": "29",
"courses": [
"iology and medical science",
"pplied biology",
"medicine",
"cell biology"
]
},
{
"id": 4,
"name": "Lucy Mu",
"sex": "female",
"age": "22",
"courses": [
"Introduction to ART ",
"sketch",
"Composition",
"sculpture"
]
}
]

這是剛開始的樣子,地址欄中默認(rèn)的path是/1,所以直接顯示了第一個學(xué)生的信息。頁碼總數(shù)也能獲得。

點(diǎn)擊了Previous按鈕后的效果。不能再往前翻頁了

處于第4頁時,點(diǎn)擊Next按鈕時的效果。不能再往后翻頁了。

在頁碼范圍內(nèi)翻頁是沒有問題的!

鑒于篇幅,我就不演示輸入的頁碼超出范圍的情況了。上面的截圖是輸入正確范圍的頁碼,點(diǎn)擊Go按鈕的效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Angular.JS內(nèi)置服務(wù)$http對數(shù)據(jù)庫的增刪改使用教程
- 三種AngularJS中獲取數(shù)據(jù)源的方式
- AngularJS實(shí)現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實(shí)例
- 基于AngularJS實(shí)現(xiàn)頁面滾動到底自動加載數(shù)據(jù)的功能
- Angularjs實(shí)現(xiàn)多個頁面共享數(shù)據(jù)的方式
- 詳解angularJs中自定義directive的數(shù)據(jù)交互
- Angular.js如何從PHP讀取后臺數(shù)據(jù)
- Angularjs 滾動加載更多數(shù)據(jù)
- 深入學(xué)習(xí)AngularJS中數(shù)據(jù)的雙向綁定機(jī)制
- 在 Angular6 中使用 HTTP 請求服務(wù)端數(shù)據(jù)的步驟詳解
- Angular.JS讀取數(shù)據(jù)庫數(shù)據(jù)調(diào)用完整實(shí)例
相關(guān)文章
Angular2里獲?。╥nput file)上傳文件的內(nèi)容的方法
這篇文章主要介紹了Angular2里獲取(input file)上傳文件的內(nèi)容的方法,非常具有實(shí)用價值,需要的朋友可以參考下2017-09-09
使用Angular CDK實(shí)現(xiàn)一個Service彈出Toast組件功能
本文主要寫用cdk實(shí)現(xiàn)一個簡單的Toast組件,使用的是cdk中的overlay模塊,需要手動安裝環(huán)境,具體安裝方法及相關(guān)實(shí)現(xiàn)代碼跟隨小編一起看看吧2021-07-07
理解Angular的providers給Http添加默認(rèn)headers
本篇文章主要介紹了理解Angular的providers給Http添加默認(rèn)headers,具有一定的參考價值,有興趣的同學(xué)可以了解一下2017-07-07
AngularJS實(shí)現(xiàn)表單驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)表單驗(yàn)證功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Angularjs的$http異步刪除數(shù)據(jù)詳解及實(shí)例
這篇文章主要介紹了Angularjs的$http異步刪除數(shù)據(jù)詳解及實(shí)例的相關(guān)資料,這里提供實(shí)現(xiàn)思路及實(shí)現(xiàn)具體的方法,需要的朋友可以參考下2017-07-07
Angular5給組件本身的標(biāo)簽添加樣式class的方法
本篇文章主要介紹了Angular 5 給組件本身的標(biāo)簽添加樣式class的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動畫的方法
本篇文章主要介紹了angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動畫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-07-07

