角度路由示例

时间:2014-12-27 11:00:06

标签: javascript angularjs

我是角度js的新手我尝试制作以下路由示例,但它没有用。 以下网址http://plnkr.co/edit/dd8Nk9PDFotCQu4yrnDg?p=preview

中的示例
// create the module and name it scotchApp
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.';
});

是索引页

 <!DOCTYPE html>
 <!-- define angular app -->
 <html ng-app="scotchApp">

 <head>
 <!-- SCROLLS -->
 <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 -->
 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
 <script src="script.js"></script>
 </head>

 <!-- define angular controller -->
 <body ng-controller="mainController">

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

 <div id="main">

 <!-- angular templating -->
    <!-- this is where content will be injected -->
 <div ng-view></div>

 </div>

  <footer class="text-center">
  <p>View the tutorial on 
  <a href="http://scotch.io/tutorials/javascript/
  single-page-apps-with-angularjs-routing-and-templating">Scotch.io</a></p>


  <p>View a tutorial on <a href="http://scotch.io/tutorials/javascript/
       animating-angularjs-apps-ngview">Animating Your Angular Single Page App</a></p>
 </footer>

 </body>

 </html>

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

由于您复制了给定链接中的确切代码,因此代码中没有任何错误。你在html中给出的路径是正确的吗?

是src =&#34; script.js&#34;你的script.js的实际位置?

答案 1 :(得分:0)

上面的代码工作正常。如果您仍然遇到任何问题,请分享错误消息。也不要忘记在客户端服务器架构上打开此应用程序。您还可以尝试使用Chrome浏览器中的任何本地服务器或插件。

以下是另一个Angular UI路由示例的链接:Angular UI Routing Example

相关问题