利用AngularJS 實現一個 Single Page App

來源:互聯網
上載者:User

標籤:

      看了下angular 的route,用它做個非常簡單的單頁面應用,記錄一下。

      順便說下,好處是,頁面改變時不需要重新整理,而每個頁面都展現不同的資料。尤其在使用模板頁的時候,非常方便。

一 樣本

  1. 項目結構:

    index.html

    script.js

    ------pages

    ---------home.html

    ---------about.html

    ---------cantact.html

  2. 項目代碼

    <1>.index.html

<!DOCTYPE html><html ng-app="scotchApp"><head><title>Angular Routing Example</title>    <link rel="stylesheet" href="libs/bootstrap/css/bootstrap.min.css" />    <link rel="stylesheet" href="libs/font-awesome-4.3.0/css/font-awesome.css" /></head><body ng-controller="mainController"><header><nav class="navbar navbar-default"><div><div>             <a href="/">Angular Routing Example</a>                </div>                <ul class="nav navbar-nav navbar-right">                    <li><a href="#home"><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>  <div id="main">        <div ng-view></div>    </div>   <script src="libs/angular1.3.15/angular.min.js"></script>    <script src="libs/jquery2.1.3/jquery.min.js"></script>    <script src="libs/angular1.3.15/angular-route.min.js"></script>    <script src="script.js"></script></body></html>

      <2>.script.js

var appModule = angular.module(‘scotchApp‘, [‘ngRoute‘]);// configure our routesappModule.config(function($routeProvider) {$routeProvider// route for the home page        .when(‘/home‘, {            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‘        })        .otherwise(‘/home‘);});var mainCtrl = function($scope){$scope.message = ‘Everyone come and see how good I look!‘;}var aboutCtrl = function($scope){$scope.message = ‘Look! I am an about page.‘;}var contactCtrl = function($scope){$scope.message = ‘Contact us! JK. This is just a demo.‘;}appModule.controller(‘mainController‘, mainCtrl);appModule.controller(‘aboutController‘, aboutCtrl);appModule.controller(‘contactController‘, contactCtrl);

      <3>.運行效果:

二 遇到的問題:

  1. 出現錯誤:Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

    解決:因為我是以檔案的形式開啟的,由於chrome 的設定問題,只支援http, data, chrome, chrome-extension, https, chrome-extension-resource 這些格式,不支援file:///xxxx.html 這種格式,所以會出現這個問題。

    方法一:啟動chrom 參數  

  2. --allow-file-access-from-files

    方法二:啟動tomcat,把你的檔案全扔到webapps 裡,然後在瀏覽器裡訪問,就可以了。


利用AngularJS 實現一個 Single Page App

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.