使用AngularJS創(chuàng)建單頁應(yīng)用的編程指引
概述
單頁應(yīng)用現(xiàn)在越來越受歡迎。模擬單頁應(yīng)用程序行為的網(wǎng)站都能提供手機/平板電腦應(yīng)用程序的感覺。Angular可以幫助我們輕松創(chuàng)建此類應(yīng)用
簡單應(yīng)用
我們打算創(chuàng)建一個簡單的應(yīng)用,涉及主頁,關(guān)于和聯(lián)系我們頁面。雖然Angular是為創(chuàng)建比這更復(fù)雜的應(yīng)用而生的,但是本教程展示了許多我們在大型項目中需要的概念。
目標(biāo)
- 單頁應(yīng)用
- 無刷新式頁面變化
- 每個頁面包含不同數(shù)據(jù)
雖然使用Javascript和Ajax可以實現(xiàn)上述功能,但是在我們的應(yīng)用中,Angular可以使我們處理更容易。
文檔結(jié)構(gòu)
- - script.js <!-- stores all our angular code -->
- - index.html <!-- main layout -->
- - pages <!-- the pages that will be injected into the main layout -->
- ----- home.html
- ----- about.html
- ----- contact.html
HTML頁面
這一部分比較簡單。我們使用Bootstrap和Font Awesome。打開你的index.html文件,然后我們利用導(dǎo)航欄,添加一個簡單布局。
<!-- index.html --> <!DOCTYPE html> <html> <head> <!-- SCROLLS --> <!-- load bootstrap and fontawesome via CDN --> <link rel="stylesheet" /> <link rel="stylesheet" /> <!-- 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 >Scotch.io</a> </footer> </body> </html>
在頁面超鏈接中,我們使用"#"。我們不希望瀏覽器認(rèn)為我們實際上是鏈接到about.html和contact.html。
Angular應(yīng)用
模型和控制器
此時我們準(zhǔn)備設(shè)置我們的應(yīng)用。讓我們先來創(chuàng)建angular模型和控制器。關(guān)于模型和控制器,請查閱文檔已獲得更多內(nèi)容。
首先,我們需要用javascript來創(chuàng)建我們的模型和控制器,我們將此操作放到script.js中:
// 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可以知道如何引導(dǎo)我們的應(yīng)用。為了測試功能有效,我們也會展示一個我們創(chuàng)建的變量$scope.message的值。
<!-- index.html --> <!DOCTYPE html> <!-- define angular app --> <html ng-app="scotchApp"> <head> <!-- SCROLLS --> <!-- load bootstrap and fontawesome via CDN --> <link rel="stylesheet" /> <link rel="stylesheet" /> <!-- SPELLS --> <!-- load angular via CDN --> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script> <script src="http://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層中,我們現(xiàn)在可以看到我們創(chuàng)建的消息。知道了我們的模型和控制器設(shè)置完畢并且Angular可以正常運行,那么我們將要開始使用這一層來展示不同的頁面。
將頁面注入到主布局中
ng-view 是一個用來包含當(dāng)前路由(/home, /about, or /contact)的模板的angular指令, 它會獲得基于特定路由的文件并將其諸如到主布局中(index.html).
我們將會想div#main中的站點加入ng-view代碼來告訴Angular將我們渲染的頁面放在哪里.
<!-- index.html --> ... <!-- MAIN CONTENT AND INJECTED VIEWS --> <div id="main"> <!-- angular templating --> <!-- this is where content will be injected --> <div ng-view></div> </div> ...
配置路由和視圖
由于我們在創(chuàng)建一個單頁應(yīng)用,并且不希望頁面刷新,那么我們會用到Angular路由的能力。
讓我們看一下我們的Angular文件,并添加到我們的應(yīng)用中。我們將會在Angular中使用$routeProvider來處理我們的路由。通過這種方式,Angular將會處理所有神奇的請求,通過取得一個新文件并將其注入到我們的布局中。
AngularJS 1.2 和路由
在1.1.6版本之后,ngRoute模型不在包含在Angular當(dāng)中。你需要通過在文檔開頭聲明該模型來使用它。該教程已經(jīng)為AngularJS1.2更新:
// 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.'; });
現(xiàn)在,我們已經(jīng)通過$routeProvider定義好了我們的路由。通過配置你會發(fā)現(xiàn),你可以使用指定路由、模板文件甚至是控制器。通過這種方法,我們應(yīng)用的每一部分都會使用Angular控制器和它自己的視圖。
清理URL: angular默認(rèn)會將一個井號放入URL中。為了避免這種事情,我們需要使用$locationProvider來啟用 HTML History API. 它將會移除掉hash并創(chuàng)建出漂亮的URL。我們的主頁將會拉取 home.html 文件. About 和 contact 頁面將會拉取它們關(guān)聯(lián)的文件. 現(xiàn)在如果我們查看我們的應(yīng)用,并點擊導(dǎo)航,我們的內(nèi)容將會照我們的意思進行改變.
要完成這個教程,我們只需要定義好將會被注入的頁面就行了. 我們也將會讓它們每一個都展示來自與他們相關(guān)的控制器的消息.
<!-- 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路由只會在你為其設(shè)置的環(huán)境后才會起效。你要確保是使用的 http://localhost 或者是某種類型的環(huán)境. 否則angular會說跨域請求支持HTTP.
Angular應(yīng)用的動畫
一旦你把所有的路由都完成之后,你就能開始把玩你的站點并向其加入動畫了. 為此,你需要使用angular提供的 ngAnimate 模塊. 后面你就可以用CSS動畫來用動畫的方式切換視圖了.
單頁面App上的SEO
理想情況下,此技術(shù)可能會被用在有用戶登錄后的應(yīng)用程序中。你當(dāng)然不會真的想要特定用戶私人性質(zhì)的頁面被搜索引擎索引. 例如,你不會想要你的讀者賬戶,F(xiàn)acebook登錄的頁面或者博客CMS頁面被索引到.
如果你確實像針對你的應(yīng)用進行SEO,那么如何讓SEO在使用js構(gòu)建頁面的應(yīng)用/站點上起效呢? 搜索引擎難于處理這些應(yīng)用程序因為內(nèi)容是由瀏覽器動態(tài)構(gòu)建的,而且對爬蟲是不可見的.
讓你的應(yīng)用對SEO友好
使得js單頁面應(yīng)用對SEO友好的技術(shù)需要定期做維護. 根據(jù)官方的Google 建議, 你需要創(chuàng)建HTML快照. 其如何運作的概述如下:
- 爬蟲會發(fā)現(xiàn)一個友好的URL(http://scotch.io/seofriendly#key=value)
- 然后爬蟲會想服務(wù)器請求對應(yīng)這個URL的內(nèi)容(用一種特殊的修改過的方式)
- Web服務(wù)器會使用一個HTML快照返回內(nèi)容
- HTML快照會被爬蟲處理
- 然后搜索結(jié)果會顯示原來的URL
更多關(guān)于這個過程的信息,可以去看看Google的 AJAX爬蟲 和他們有關(guān)創(chuàng)建HTML快照 的指南.
相關(guān)文章
Angular4實現(xiàn)動態(tài)添加刪除表單輸入框功能
這篇文章主要介紹了Angular4實現(xiàn)動態(tài)添加刪除表單輸入框功能,需要的朋友可以參考下2017-08-08詳解AngularJs中$resource和restfu服務(wù)端數(shù)據(jù)交互
之前小編和大家分享過使用$http同服務(wù)器進行通信,但是功能上比較簡單,angularjs還提供了另外一個可選的服務(wù)$resource,使用它可以非常方便的同支持restful的服務(wù)單進行數(shù)據(jù)交互。下面來一起看看吧。2016-09-09關(guān)于AngularJs數(shù)據(jù)的本地存儲詳解
本文主要介紹了每一個獨立的JS文件或者不同的控制器如何實現(xiàn)數(shù)據(jù)的共享與交互的方法。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01AngularJS實現(xiàn)標(biāo)簽頁的兩種方式
這篇文章分別給大家介紹了AngularJS實現(xiàn)標(biāo)簽頁的兩種方式,一種是通過普通指令實現(xiàn)標(biāo)簽頁,另外一種是通過自定義指令實現(xiàn)的標(biāo)簽頁,有需要的朋友們可以來參考借鑒,下面來一起看看吧。2016-09-09Angular 2.x學(xué)習(xí)教程之結(jié)構(gòu)指令詳解
結(jié)構(gòu)指令通過添加和刪除 DOM 元素來更改 DOM 布局。Angular 中兩個常見的結(jié)構(gòu)指令是 *ngIf 和 *ngFor,下面這篇文章主要給大家介紹了關(guān)于Angular 2.x結(jié)構(gòu)指令的相關(guān)資料,需要的朋友可以參考下。2017-05-05