AngularJS:ngRoute,否则不起作用
我有一个非常奇怪的错误,我设置了我的路线和控制器。现在我只有一个没有错误的空白页 index.htmlAngularJS:ngRoute,否则不起作用,angularjs,ngroute,Angularjs,Ngroute,我有一个非常奇怪的错误,我设置了我的路线和控制器。现在我只有一个没有错误的空白页 index.html <!DOCTYPE html> <html ng-app="RekenTalent" lang="nl"> <head> <title>Rekentalent.nl: Ben jij een talent in Rekenen?</title> <!-- Stylesheets -->
<!DOCTYPE html>
<html ng-app="RekenTalent" lang="nl">
<head>
<title>Rekentalent.nl: Ben jij een talent in Rekenen?</title>
<!-- Stylesheets -->
<link rel="stylesheet" type="text/css" href="/app/front/assets/stylesheets/style.css">
<!-- AngularJS -->
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular-route.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular-animate.min.js"></script>
<!-- Controllers -->
<script src="/app/front/controllers/controller.js"></script>
<!-- Router -->
<script src="/app/front/router.js"></script>
</head>
<body ng-view></body>
</html
Router.js:
var RekenTalent = angular.module('RekenTalent', [
'ngRoute', 'RekenTalentControllers'
]);
RekenTalent.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/index', {
templateUrl: '/templates/index.html',
controller: 'rekenCtrl'
}).
otherwise({
redirect: '/index'
});
}]);
和/templates/index.html只显示“hi”。有人知道我为什么会得到一张空白页吗?它应该将我重定向到/index.html,并且/index.html应该使用/templates/index.html作为模板。问题和解决方法是什么
更新:
当我转到/index时,它工作了,所以其他参数不工作,为什么不呢?找到了它;重定向应该被重定向到
var RekenTalent = angular.module('RekenTalent', [
'ngRoute', 'RekenTalentControllers'
]);
RekenTalent.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/index', {
templateUrl: '/app/front/templates/index.html',
controller: 'rekenCtrl'
}).
otherwise({
redirectTo: '/index'
});
}]);
将
重定向
更改为重定向
最好使用home.template来避免所有此类问题,我的意思是考虑:
您有一个index.html
,它包含了所有包含的脚本,比如angular、jquery等等,在body标记中有ng视图
,好吗
现在,如果您有任何模板,例如欢迎或任何用于索引的模板,请在home.html
中编写该模板,好吗?像这样:
index.html:
<body ng-app="yourapp">
<div ng-view>
</div>
</body>
最好将所有模板放在partials文件夹中,父文件夹中包含index.html,如下所示
root
yourAppFolder
1-Assets
Css
Js
2-Partials
home.html
login.html
3-index.html
我有以下代码示例,例如:
.config(['$routeProvider',
function($routeProvider) {
console.log('routeando');
//alert('otherwise' + $routeProvider.route);
$routeProvider.
when('/', { templateUrl: 'home'}).
when('/route1', { templateUrl: 'route1'}).
/*Without the next line, redirectTo doesnt work*/
when('/error', { templateUrl: 'error'}).
otherwise({ redirectTo: '/error'});
}]);
否则,只有当您已经为该路由设置了“when”参数时,它才会起作用
请原谅我的英语,祝你愉快。我认为这是文件引用错误,请告诉我们,你的索引在哪里,你的PC中的角度脚本在哪里,你可以在你的控制器中添加一个console.log('load'),检查控制器是否正确启动,但仍然,我认为它的reference errorindex.html=>/app/front/templates/index.html,同样当我把console.log('hi')放在控制器中时,我在控制台中没有日志。奇怪的是,当我将/index改为/在路由器中工作时,为什么不工作?
yourapp.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'partials/home.html',
controller: 'homeCtrl'
})
.otherwise({redirectTo:'/'});
}
root
yourAppFolder
1-Assets
Css
Js
2-Partials
home.html
login.html
3-index.html
.config(['$routeProvider',
function($routeProvider) {
console.log('routeando');
//alert('otherwise' + $routeProvider.route);
$routeProvider.
when('/', { templateUrl: 'home'}).
when('/route1', { templateUrl: 'route1'}).
/*Without the next line, redirectTo doesnt work*/
when('/error', { templateUrl: 'error'}).
otherwise({ redirectTo: '/error'});
}]);