为什么路由不能在这段代码中工作?

时间:2015-02-06 22:00:26

标签: javascript angularjs url-routing

这只是一个需要学习AngularJS的小项目,而且我无法让路由工作。根据每个教程,我已经查看了我在这里应该工作的内容,但不幸的是,它并没有。如果有人能帮助我找到问题并帮我解决问题,我将非常感激。

这是我的索引:



<!DOCTYPE html>
    <html ng-app="danApp">
        <head>
            <script src="Scripts/angular.min.js"></script>
            <script src="Scripts/test.js"></script>
        </head>
        <body>
            <div ng-view></div>
        </body>
    </html>
&#13;
&#13;
&#13;

查看1

&#13;
&#13;
<h1>View 1</h1>
<br><br><br><br>
<div>
   <input type="text" ng-model="nameText" >
      <ul>
         <li ng-repeat="things in info | filter: nameText | orderBy:'name'">
            Name: {{ things.name }} <br> Age: {{ things.age }} <br>
            City: {{ things.city }}
            <br>
         </li>
      </ul>
</div>
&#13;
&#13;
&#13;

查看2

&#13;
&#13;
<h1>View 2</h1>
<br><br><br><br>
<div>
    <input type="text" ng-model="nameText" >
      <ul>
         <li ng-repeat="stuff in parents | filter: nameText | orderBy:'name'">
            Name: {{ stuff.name }} <br> Age: {{ stuff.age }} <br>
            City: {{ stuff.city }}
            <br>
         </li>
      </ul>
</div>
&#13;
&#13;
&#13;

最后,我的模块控制器路由代码

&#13;
&#13;
"use strict";

 var danApp = angular.module('danApp', []);
               
 danApp.controller("danCtrl", function($scope) {
    $scope.info = [
        {name: 'Daniel', age: '22', city: 'Clarksville'},
        {name: 'Derek', age: '19', city: 'Jacksonville'},
        {name: 'Emily', age: '18', city: 'Erin'},
        {name: 'Denise', age: '27', city: 'Phoenix'}, 
    ];
 });
               
               
 danApp.controller("danCtrl2", function($scope) {
   $scope.parents = [
      {name: 'Lathan', age: '54', city: 'Stewart'},
      {name: 'Candy', age: '54', city: 'Stewart'},
      {name: 'Christine', age: '43', city: 'Erin'}
   ];
 });           
               
               
danApp.config(function ($routeProvider) {
   $routeProvider
      .when('/',
      {
         controller: 'danCtrl',
         templateUrl: 'views/view1.html'
      })
      .when('/view2',
      {
         controller: 'danCtrl2',
         templateUrl: 'views/view2.html'
      })
      .otherwise({redirectTo: '/'});
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

问题是您需要添加angular-route.jsangular-route.min.js脚本,该脚本可以从Angular website下载。在index.html中添加脚本:

<script src="Scripts/angular-route.min.js"></script>

然后需要将模块添加到您的模块中:

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

Plunkr