使用AngularJS建立單頁應用的編程指引
這篇文章主要介紹了使用AngularJS建立單頁應用的編程指引,AngularJS是一款高人氣的JavaScript庫,需要的朋友可以參考下
概述
單頁應用現在越來越受歡迎。類比單頁應用程式行為的網站都能提供手機/平板電腦應用程式的感覺。Angular可以協助我們輕鬆建立此類應用
簡單應用
我們打算建立一個簡單的應用,涉及首頁,關於和聯絡我們頁面。雖然Angular是為建立比這更複雜的應用而生的,但是本教程展示了許多我們在大型項目中需要的概念。
目標
單頁應用
無重新整理式頁面變化
每個頁麵包含不同資料
雖然使用Javascript和Ajax可以實現上述功能,但是在我們的應用中,Angular可以使我們處理更容易。
文檔結構
- script.js
- index.html
- pages
----- home.html
----- about.html
----- contact.html
HTML頁面
這一部分比較簡單。我們使用Bootstrap和Font Awesome。開啟你的index.html檔案,然後我們利用導覽列,添加一個簡單布局。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 |
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<!-- SCROLLS -->
<!-- load bootstrap and fontawesome via CDN -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
<!-- SPELLS -->
<!-- load angular via CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<!-- HEADER AND NAVBAR -->
<header>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">Angular Routing Example</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><i class="fa fa-home"></i> Home</a></li>
<li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
<li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
</ul>
</div>
</nav>
</header>
<!-- MAIN CONTENT AND INJECTED VIEWS -->
<div id="main">
<!-- angular templating -->
<!-- this is where content will be injected -->
</div>
<!-- FOOTER -->
<footer class="text-center">
View the tutorial on <a href="http://scotch.io/tutorials/angular-routing-and-templating-tutorial">Scotch.io</a>
</footer>
</body>
</html>
|
在頁面超連結中,我們使用"#"。我們不希望瀏覽器認為我們實際上是連結到about.html和contact.html。
Angular應用
模型和控制器
此時我們準備設定我們的應用。讓我們先來建立angular模型和控制器。關於模型和控制器,請查閱文檔已獲得更多內容。
首先,我們需要用javascript來建立我們的模型和控制器,我們將此操作放到script.js中:
?
|
1
2
3
4
5
6
7
8
9
10
11 |
// script.js
// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', []);
// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {
// create a message to display in our view
$scope.message = 'Everyone come and see how good I look!';
});
|
接下來讓我們把模型和控制器添加到我們的HTML頁面中,這樣Angular可以知道如何引導我們的應用。為了測試功能有效,我們也會展示一個我們建立的變數$scope.message的值。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 |
<!-- index.html -->
<!DOCTYPE html>
<!-- define angular app -->
<html ng-app="scotchApp">
<head>
<!-- SCROLLS -->
<!-- load bootstrap and fontawesome via CDN -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
<!-- SPELLS -->
<!-- load angular via CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular-route.js"></script>
<script src="script.js"></script>
</head>
<!-- define angular controller -->
<body ng-controller="mainController">
...
<!-- MAIN CONTENT AND INJECTED VIEWS -->
<div id="main">
{{ message }}
<!-- angular templating -->
<!-- this is where content will be injected -->
</div>
|
在main這個div層中,我們現在可以看到我們建立的訊息。知道了我們的模型和控制器設定完畢並且Angular可以正常運行,那麼我們將要開始使用這一層來展示不同的頁面。
將頁面注入到主布局中
ng-view 是一個用來包含當前路由(/home, /about, or /contact)的模板的angular指令, 它會獲得基於特定路由的檔案並將其諸如到主布局中(index.html).
我們將會想div#main中的網站加入ng-view代碼來告訴Angular將我們渲染的頁面放在哪裡.
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
<!-- index.html -->
...
<!-- MAIN CONTENT AND INJECTED VIEWS -->
<div id="main">
<!-- angular templating -->
<!-- this is where content will be injected -->
<div ng-view></div>
</div>
...
|
配置路由和視圖
由於我們在建立一個單頁應用,並且不希望頁面重新整理,那麼我們會用到Angular路由的能力。
讓我們看一下我們的Angular檔案,並添加到我們的應用中。我們將會在Angular中使用$routeProvider來處理我們的路由。通過這種方式,Angular將會處理所有神奇的請求,通過取得一個新檔案並將其注入到我們的布局中。
AngularJS 1.2 和路由
在1.1.6版本之後,ngRoute模型不在包含在Angular當中。你需要通過在文檔開頭聲明該模型來使用它。該教程已經為AngularJS1.2更新:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 |
// script.js
// create the module and name it scotchApp
// also include ngRoute for all our routing needs
var scotchApp = angular.module('scotchApp', ['ngRoute']);
// configure our routes
scotchApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
// route for the about page
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController'
});
});
// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {
// create a message to display in our view
$scope.message = 'Everyone come and see how good I look!';
});
scotchApp.controller('aboutController', function($scope) {
$scope.message = 'Look! I am an about page.';
});
scotchApp.controller('contactController', function($scope) {
$scope.message = 'Contact us! JK. This is just a demo.';
});
|
現在,我們已經通過$routeProvider定義好了我們的路由。通過配置你會發現,你可以使用指定路由、模板檔案甚至是控制器。通過這種方法,我們應用的每一部分都會使用Angular控制器和它自己的視圖。
清理URL: angular預設會將一個井號放入URL中。為了避免這種事情,我們需要使用$locationProvider來啟用 HTML History API. 它將會移除掉hash並建立出漂亮的URL。我們的首頁將會拉取 home.html 檔案. About 和 contact 頁面將會拉取它們關聯的檔案. 現在如果我們查看我們的應用,並點擊導航,我們的內容將會照我們的意思進行改變.
要完成這個教程,我們只需要定義好將會被注入的頁面就行了. 我們也將會讓它們每一個都展示來自與他們相關的控制器的訊息.
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
<!-- home.html -->
<div class="jumbotron text-center">
<h1>Home Page</h1>
<p>{{ message }}</p>
</div>
<!-- about.html -->
<div class="jumbotron text-center">
<h1>About Page</h1>
<p>{{ message }}</p>
</div>
<!-- contact.html -->
<div class="jumbotron text-center">
<h1>Contact Page</h1>
<p>{{ message }}</p>
</div>
|
本地運行: Angular路由只會在你為其設定的環境後才會起效。你要確保是使用的 http://localhost 或者是某種類型的環境. 否則angular會說跨域請求支援HTTP.
Angular應用的動畫
一旦你把所有的路由都完成之後,你就能開始把玩你的網站並向其加入動畫了. 為此,你需要使用angular提供的 ngAnimate 模組. 後面你就可以用CSS動畫來用動畫的方式切換視圖了.
單頁面App上的SEO
理想情況下,此技術可能會被用在有使用者登入後的應用程式中。你當然不會真的想要特定使用者私人性質的頁面被搜尋引擎索引. 例如,你不會想要你的讀者賬戶,Facebook登入的頁面或者部落格CMS頁面被索引到.
如果你確實像針對你的應用進行SEO,那麼如何讓SEO在使用js構建頁面的應用/網站上起效呢? 搜尋引擎難於處理這些應用程式因為內容是由瀏覽器動態構建的,而且對爬蟲是不可見的.
讓你的應用對SEO友好
使得js單頁面應用對SEO友好的技術需要定期做維護. 根據官方的Google 建議, 你需要建立HTML快照. 其如何運作的概述如下:
爬蟲會發現一個友好的URL(http://scotch.io/seofriendly#key=value)
然後爬蟲會想伺服器請求對應這個URL的內容(用一種特殊的修改過的方式)
Web伺服器會使用一個HTML快照返回內容
HTML快照會被爬蟲處理
然後搜尋結果會顯示原來的URL