$routeProvider在AngularJS中无法正常工作(导致网站崩溃)
$routeProvider not working properly in AngularJS (crashing the website)
我遵循了一些关于使用Angular的$routeProvider
的随机YT教程,结果与视频相反,对我不起作用。相反,我得到的是网站崩溃,这个错误记录在Chrome的控制台中:Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=site&p1=Error%3A%20…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.4.8%2Fangular.min.js%3A41%3A249)
通过跟踪,我发现"$routeProvider"有问题,但如果我知道的话,我会大吃一惊。这是我的代码:
var site = angular.module('site', []).
config(function($routeProvider){
$routeProvider.
when('/home', {template:'/pages/home.html'}).
when('/', {template:'/pages/home.html'}).
when('/in-play', {template:'/pages/in-play.html'}).
when('/popular', {template:'/pages/popular.html'}).
otherwise({redirectTo:'/home', tempalte:'/pages/home.html'});
});
function MainCtrl($scope, $location) {
$scope.setRoute = function(route) {
$location.path(route);
};
};
下面是所有的HTML(我也在使用ng-include
):
<!DOCTYPE html>
<html ng-app="site">
<head>
<link rel="stylesheet" href="css/style.css">
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="scripts/app.js"></script>
<title>Pre-Demo</title>
</head>
<body ng-controller="mainStaysCtrl">
<header class="header" ng-include="'pages/header.html'"></header>
<nav class="nav" ng-include="'pages/nav.html'"></nav>
<div class="main">
<div class="left-col">
<div class="quick links" ng-include="'pages/quick_links.html'"> </div>
<div class="quick-inplay links" ng-include="'pages/quick_inplay_links.html'"></div>
<div class="winner links" ng-include="'pages/winner_links.html'"></div>
</div>
<div class="center-col" ng-controller="mainCtrl">
<div class="wraper" ng-view ng-controller="jsonCtrl"></div>
</div>
<div class="right-col">
<div class="expert-mixer" ng-include="'pages/mixer.html'"></div>
<div ng-include="'pages/twitter.html'"></div>
</div>
</div>
</body>
</html>
//included page that has the call for $routeProvider
<div class="events">
<div class="bullet">
<div class="bullet-padding">
<div ng-click="setRoute('in-play')" class="bullet-link">In-Play Links</div>
</div>
</div>
</div>
有人能告诉我怎么了吗?
编辑
安蒂加回答后,我得到它加载页面。除了要加载ng-view
并且首先为其设置$routeProvider
的内容之外的所有内容。这是更新后的代码:
var site = angular.module('site', ['ngRoute']).
config(function($routeProvider){
$routeProvider.
when('/home', {templateUrl:'/pages/home.html'}).
when('/', {templateUrl:'/pages/home.html'}).
when('/in-play', {templateUrl:'/pages/in-play.html'}).
when('/popular', {templateUrl:'/pages/popular.html'}).
otherwise({redirectTo:'/home', tempalteUrl:'/pages/home.html'});
});
site.controller('mainStaysCtrl', function($scope) {
$scope.setRoute = function(route) {
$location.path(route);
};
});
和HTML
<body ng-controller="mainStaysCtrl">
<header class="header" ng-include="'pages/header.html'"></header>
<nav class="nav" ng-include="'pages/nav.html'"></nav>
<div class="main">
<div class="left-col">
<div class="quick links" ng-include="'pages/quick_links.html'"></div>
<div class="quick-inplay links" ng-include="'pages/quick_inplay_links.html'"></div>
<div class="winner links" ng-include="'pages/winner_links.html'"></div>
</div>
<div class="center-col">
<div class="wraper" ng-view ></div>
</div>
<div class="right-col">
<div class="expert-mixer" ng-include="'pages/mixer.html'"></div>
<div ng-include="'pages/twitter.html'"></div>
</div>
</div>
您没有包括路由模块。
在这里阅读,以便您首先真正理解:https://docs.angularjs.org/api/ngRoute
在包含angular.min.js
:后添加此项
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
然后在你的主应用程序模块中包括依赖项:
var site = angular.module('site', ['ngRoute']).
好吧,我在Antiga的帮助下完成了它。
问题很少(但很小)。我不得不扩展templateUrl路径以包括项目文件夹。
when('/home', {templateUrl: '/ProjectName/pages/home.html', controller: 'mainStaysCtrl'}).
另一件事是,我只是忘记了在控制器中包含$location
。
site.controller('mainStaysCtrl', function($scope, $location) {
$scope.setRoute = function(route) {
$location.path(route);
};
});
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- twitter引导崩溃在第一次点击后停止工作
- AJAX 查询工作 50+ 次,然后崩溃 JavaScript
- JavaScript代码首先使用setTimeout工作,但随后崩溃或不起作用
- React Native - Android应用程序在调试上工作,在发布时崩溃
- 将大约 300 张画布图像保存在一个数组中并发送一次进行处理,在 chrome 中崩溃,但在 Firefox 中工作正常
- 导航栏崩溃无法工作-Bootstrap+Github
- 打开时工作,但重新加载时崩溃
- 导航崩溃无法工作[Twitter Bootstrap]
- $routeProvider在AngularJS中无法正常工作(导致网站崩溃)
- Web工作程序在传递具有数组缓冲区的大型对象数组时会崩溃Chrome选项卡
- Javascript崩溃HTML -我的代码不工作,你能看到任何错误吗?
- 切换/滑动展开工作,但崩溃不与HTML箭头工作
- Javascript崩溃不工作
- 我的正则表达式在在线编辑器中工作,但在FireBug中,它使浏览器崩溃
- 当从移动设备访问时,引导导航条不会崩溃,当我缩小桌面浏览器时也能正常工作
- 元素排序例程在Firefox中工作,但在Chrome中崩溃
- jquery的崩溃都不工作时,与HAML格式结合
- 隐藏在引导-崩溃不工作