質問

I'm learning AngularJS, and I'm having issues with getting a simple routeProvider working. It's pretty basic, so I'm confused as to what I'm missing. I've triple-checked syntax and spelling, and I can't find anything missing. Help please!

Here's a Plunker: http://plnkr.co/edit/CUV3ZWXyd36Gamgn8DH0?p=preview

HTML:

<html ng-app="app">

  <head>
    <link rel="stylesheet" href="style.css" />
  </head>

  <body>
    <a href="#/page1">Page 1</a>
    <a href="#/page2">Page 2</a>
    <div ng-view></div>
  </body>

  <script data-require="angular.js@*" data-semver="1.2.14" src="http://code.angularjs.org/1.2.14/angular.js"></script>
  <script src="script.js"></script>  

</html>

Script:

var app = angular.module('app', []);

app.config(function ($routeProvider) {
  $routeProvider
      .when('/',
          {
              templateUrl: 'page1.html'
          })
      .when('/page2',
          {
              templateUrl: 'page2.html'
          })
      .otherwise(
          {   redirectTo: '/' 
          });
});
役に立ちましたか?

解決

UPDATE

because ngRoute has been moved into its own module recently that's why some outdated tutorial did not mention it...


You need to inject ngRoute to your application...

var app = angular.module('app', ['ngRoute']);

and of course add ngRoute js file to your html..

here is PLUNKER

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top