简体   繁体   English

为什么路由在此代码中不起作用?

[英]Why won't the routing work in this code?

This is just a little project to learn AngularJS , and I'm having trouble getting the routing to work. 这只是一个学习AngularJS的小项目,我在使路由正常工作方面遇到了麻烦。 According to every tutorial I've looked over what I have here should work, but it doesn't, unfortunately. 根据每篇教程的介绍,我研究了这里的工具应该可以工作,但是不幸的是,它没有用。 I would really appreciate it if someone could help me find my problem and help me fix it. 如果有人可以帮助我找到问题并帮助我解决问题,我将不胜感激。

Here is my index: 这是我的索引:

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

View 1 查看1

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

View 2 查看2

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

And, finally, my module , controllers , and routing code 最后,我的模块控制器路由代码

 "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: '/'}); }); 

The issue is you need to include the angular-route.js or angular-route.min.js script which can be downloaded from the Angular website . 问题是您需要包含可以从Angular网站下载的angular-route.jsangular-route.min.js脚本。 In index.html add the script: 在index.html中添加脚本:

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

The module then needs to be added to your module: 然后需要将该模块添加到您的模块中:

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

Plunkr Plunkr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM