最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用AngularJS創(chuàng)建單頁應(yīng)用的編程指引

 更新時(shí)間:2015年06月19日 14:53:41   投稿:goldensun  
這篇文章主要介紹了使用AngularJS創(chuàng)建單頁應(yīng)用的編程指引,AngularJS是一款高人氣的JavaScript庫,需要的朋友可以參考下

概述

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

我們打算創(chuàng)建一個(gè)簡單的應(yīng)用,涉及主頁,關(guān)于和聯(lián)系我們頁面。雖然Angular是為創(chuàng)建比這更復(fù)雜的應(yīng)用而生的,但是本教程展示了許多我們?cè)诖笮晚?xiàng)目中需要的概念。
目標(biāo)

  •         單頁應(yīng)用
  •         無刷新式頁面變化
  •         每個(gè)頁面包含不同數(shù)據(jù)

雖然使用Javascript和Ajax可以實(shí)現(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)航欄,添加一個(gè)簡單布局。
 

<!-- 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)為我們實(shí)際上是鏈接到about.html和contact.html。
Angular應(yīng)用
模型和控制器

此時(shí)我們準(zhǔn)備設(shè)置我們的應(yīng)用。讓我們先來創(chuàng)建angular模型和控制器。關(guān)于模型和控制器,請(qǐng)查閱文檔已獲得更多內(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)用。為了測(cè)試功能有效,我們也會(huì)展示一個(gè)我們創(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這個(gè)div層中,我們現(xiàn)在可以看到我們創(chuàng)建的消息。知道了我們的模型和控制器設(shè)置完畢并且Angular可以正常運(yùn)行,那么我們將要開始使用這一層來展示不同的頁面。

將頁面注入到主布局中

ng-view 是一個(gè)用來包含當(dāng)前路由(/home, /about, or /contact)的模板的angular指令, 它會(huì)獲得基于特定路由的文件并將其諸如到主布局中(index.html).

我們將會(huì)想div#main中的站點(diǎn)加入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>
 
...

配置路由和視圖

由于我們?cè)趧?chuàng)建一個(gè)單頁應(yīng)用,并且不希望頁面刷新,那么我們會(huì)用到Angular路由的能力。

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

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定義好了我們的路由。通過配置你會(huì)發(fā)現(xiàn),你可以使用指定路由、模板文件甚至是控制器。通過這種方法,我們應(yīng)用的每一部分都會(huì)使用Angular控制器和它自己的視圖。


清理URL: angular默認(rèn)會(huì)將一個(gè)井號(hào)放入U(xiǎn)RL中。為了避免這種事情,我們需要使用$locationProvider來啟用 HTML History API. 它將會(huì)移除掉hash并創(chuàng)建出漂亮的URL。我們的主頁將會(huì)拉取 home.html 文件. About 和 contact 頁面將會(huì)拉取它們關(guān)聯(lián)的文件. 現(xiàn)在如果我們查看我們的應(yīng)用,并點(diǎn)擊導(dǎo)航,我們的內(nèi)容將會(huì)照我們的意思進(jìn)行改變.

要完成這個(gè)教程,我們只需要定義好將會(huì)被注入的頁面就行了. 我們也將會(huì)讓它們每一個(gè)都展示來自與他們相關(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>

本地運(yùn)行: Angular路由只會(huì)在你為其設(shè)置的環(huán)境后才會(huì)起效。你要確保是使用的 http://localhost 或者是某種類型的環(huán)境. 否則angular會(huì)說跨域請(qǐng)求支持HTTP.

Angular應(yīng)用的動(dòng)畫

一旦你把所有的路由都完成之后,你就能開始把玩你的站點(diǎn)并向其加入動(dòng)畫了. 為此,你需要使用angular提供的 ngAnimate 模塊. 后面你就可以用CSS動(dòng)畫來用動(dòng)畫的方式切換視圖了. 
單頁面App上的SEO

理想情況下,此技術(shù)可能會(huì)被用在有用戶登錄后的應(yīng)用程序中。你當(dāng)然不會(huì)真的想要特定用戶私人性質(zhì)的頁面被搜索引擎索引. 例如,你不會(huì)想要你的讀者賬戶,F(xiàn)acebook登錄的頁面或者博客CMS頁面被索引到.

如果你確實(shí)像針對(duì)你的應(yīng)用進(jìn)行SEO,那么如何讓SEO在使用js構(gòu)建頁面的應(yīng)用/站點(diǎn)上起效呢? 搜索引擎難于處理這些應(yīng)用程序因?yàn)閮?nèi)容是由瀏覽器動(dòng)態(tài)構(gòu)建的,而且對(duì)爬蟲是不可見的.

讓你的應(yīng)用對(duì)SEO友好

使得js單頁面應(yīng)用對(duì)SEO友好的技術(shù)需要定期做維護(hù). 根據(jù)官方的Google 建議, 你需要?jiǎng)?chuàng)建HTML快照. 其如何運(yùn)作的概述如下:

  •     爬蟲會(huì)發(fā)現(xiàn)一個(gè)友好的URL(http://scotch.io/seofriendly#key=value)
  •     然后爬蟲會(huì)想服務(wù)器請(qǐng)求對(duì)應(yīng)這個(gè)URL的內(nèi)容(用一種特殊的修改過的方式)
  •     Web服務(wù)器會(huì)使用一個(gè)HTML快照返回內(nèi)容
  •     HTML快照會(huì)被爬蟲處理
  •     然后搜索結(jié)果會(huì)顯示原來的URL

更多關(guān)于這個(gè)過程的信息,可以去看看Google的 AJAX爬蟲 和他們有關(guān)創(chuàng)建HTML快照 的指南.

相關(guān)文章

最新評(píng)論

磐石市| 德阳市| 浏阳市| 德州市| 邓州市| 竹北市| 丁青县| 桂林市| 扎鲁特旗| 安顺市| 泰州市| 恭城| 天门市| 诸暨市| 弥勒县| 卢湾区| 衡东县| 永胜县| 孝义市| 芜湖县| 岳阳市| 桃园市| 右玉县| 乌兰浩特市| 遵义市| 宁强县| 陈巴尔虎旗| 广灵县| 蓬溪县| 临沂市| 长岛县| 新密市| 彭泽县| 洛宁县| 长乐市| 通化市| 静乐县| 乡宁县| 杂多县| 永德县| 商城县|