javascript - Angular 路由示例

标签 javascript angularjs

我是 Angular 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>

谁能帮帮我吗?

最佳答案

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

src="script.js"是 script.js 的实际位置吗?

关于javascript - Angular 路由示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666599/

相关文章:

angularjs - 动态更改Recaptcha V2语言

javascript - 根据 CHANGELOG,由于版本 3.53.0 Phaser3 正在阻止 `AudioContext was not allowed` 警告,但我仍然遇到它,我该怎么办?

javascript - spy 功能不起作用 - Jasmine

javascript - MEAN stack Angular 显示 Express 中的对象,但不显示 HTML

javascript - uib-btn-radio 不适用于 $scope 但适用于 "controller as"语法?

css - 如何在 angularjs 中为页面的一部分制作加载掩码?

javascript - AngularJS:在每次按键时使用动态 ng-model 模糊更新输入

javascript - 添加一个函数来移动拼图的碎片

javascript - 需要代码返回数组坐标

javascript - jQuery 函数不返回