gpt4 book ai didi

javascript - AngularJS ngRoute 不工作

转载 作者:行者123 更新时间:2023-12-03 12:39:35 26 4
gpt4 key购买 nike

我有四个文件:

angularjs.js
scripts.js
ngroute.js
index.html

现在,我配置了以下路由:

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:

<!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 链接时,它不会加载 View 。即使上面的routeConfig代码也会导致js脚本的其余部分停止工作。 block 中有问题,但我不知道它是什么!

最佳答案

otherwise 部分存在语法错误。将您的路线配置更改为:

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

关于javascript - AngularJS ngRoute 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23578272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com