国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

使用AngularJS創建單頁應用的編程指引

2019-11-20 12:12:21
字體:
來源:轉載
供稿:網友

概述

單頁應用現在越來越受歡迎。模擬單頁應用程序行為的網站都能提供手機/平板電腦應用程序的感覺。Angular可以幫助我們輕松創建此類應用
簡單應用

我們打算創建一個簡單的應用,涉及主頁,關于和聯系我們頁面。雖然Angular是為創建比這更復雜的應用而生的,但是本教程展示了許多我們在大型項目中需要的概念。
目標

  •         單頁應用
  •         無刷新式頁面變化
  •         每個頁面包含不同數據

雖然使用Javascript和Ajax可以實現上述功能,但是在我們的應用中,Angular可以使我們處理更容易。
文檔結構

  • - 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文件,然后我們利用導航欄,添加一個簡單布局。
 

<!-- 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>

在頁面超鏈接中,我們使用"#"。我們不希望瀏覽器認為我們實際上是鏈接到about.html和contact.html。
Angular應用
模型和控制器

此時我們準備設置我們的應用。讓我們先來創建angular模型和控制器。關于模型和控制器,請查閱文檔已獲得更多內容。

首先,我們需要用javascript來創建我們的模型和控制器,我們將此操作放到script.js中:
 

// script.js // create the module and name it scotchAppvar scotchApp = angular.module('scotchApp', []); // create the controller and inject Angular's $scopescotchApp.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的值。
 

<!-- 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層中,我們現在可以看到我們創建的消息。知道了我們的模型和控制器設置完畢并且Angular可以正常運行,那么我們將要開始使用這一層來展示不同的頁面。

將頁面注入到主布局中

ng-view 是一個用來包含當前路由(/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> ...

配置路由和視圖

由于我們在創建一個單頁應用,并且不希望頁面刷新,那么我們會用到Angular路由的能力。

讓我們看一下我們的Angular文件,并添加到我們的應用中。我們將會在Angular中使用$routeProvider來處理我們的路由。通過這種方式,Angular將會處理所有神奇的請求,通過取得一個新文件并將其注入到我們的布局中。

AngularJS 1.2 和路由

在1.1.6版本之后,ngRoute模型不在包含在Angular當中。你需要通過在文檔開頭聲明該模型來使用它。該教程已經為AngularJS1.2更新:
 

// script.js // create the module and name it scotchApp  // also include ngRoute for all our routing needsvar scotchApp = angular.module('scotchApp', ['ngRoute']); // configure our routesscotchApp.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 $scopescotchApp.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 頁面將會拉取它們關聯的文件. 現在如果我們查看我們的應用,并點擊導航,我們的內容將會照我們的意思進行改變.

要完成這個教程,我們只需要定義好將會被注入的頁面就行了. 我們也將會讓它們每一個都展示來自與他們相關的控制器的消息.
 

<!-- 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

更多關于這個過程的信息,可以去看看Google的 AJAX爬蟲 和他們有關創建HTML快照 的指南.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北海市| 祁阳县| 聂荣县| 资源县| 台北县| 宁陕县| 吉安市| 红桥区| 新建县| 永仁县| 彰武县| 绍兴市| 凤山市| 田阳县| 五河县| 普安县| 惠来县| 韶山市| 全椒县| 崇左市| 蒙城县| 高雄市| 定安县| 南江县| 中卫市| 辽源市| 南丹县| 龙游县| 穆棱市| 舟山市| 苏尼特右旗| 宣武区| 岫岩| 台湾省| 民县| 德令哈市| 克什克腾旗| 东乡| 谢通门县| 陆河县| 布拖县|