AngularJS ngRoute不工作 [英] AngularJS ngRoute is not working
本文介绍了AngularJS ngRoute不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有forur文件:
angularjs.js
scripts.js
ngroute.js
index.html
现在,我甲肝配置了以下路线:
Now, I hav configured the following route:
var myApp = angular.module("myApp", ['ngRoute']);
myApp.config(function($routeProvider){
$routeProvider
.when("/author/add", {
controller : "AuthorController",
templateUrl : "view2.html"
})
.when("/author/show", {
controller : "AuthorController",
templateUrl : "view1.html"
})
.otherwise("/", redirectTo : "/author/add")
});
和这里是我的index.html:
And here is my index.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="angular.js" ></script>
<script type="text/javascript" src="ngroute.js" ></script>
<script type="text/javascript" src="scripts.js" ></script>
</head>
<body data-ng-app="myApp">
<h1>Hello</h1>
<div data-ng-view=""></div>
<div data-ng-controller="AuthorController">
<ul data-ng-repeat="single in authors">
Author {{ single.author }} has written {{ author.book }}
</ul>
</div>
<a href="#/author/add">Add New Author</a>
</body>
</html>
但是,当我点击作者/加HREF链接,它不会加载该视图。即使在上述routeConfig code导致JS脚本的其余部分停止工作。有块中的问题,但我无法弄清楚它是什么!
But when I click on author/add href link, it does not load the view. Even the above routeConfig code cause the rest of js script to stop working. There is a problem in the block, but I can't figure out what it is!
推荐答案
您已经在,否则
部分语法错误。路线配置更改为:
You have a syntax error in the otherwise
part. Change your route config to:
myApp.config(function($routeProvider){
$routeProvider
.when("/author/add", {
controller : "AuthorController",
templateUrl : "view2.html"
})
.when("/author/show", {
controller : "AuthorController",
templateUrl : "view1.html"
})
.otherwise({redirectTo : "/author/add"})
});
这篇关于AngularJS ngRoute不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文