gpt4 book ai didi

javascript - AngularJS 路由配置

转载 作者:行者123 更新时间:2023-11-28 13:43:08 26 4
gpt4 key购买 nike

我是 angularjs 的新手,只是想用它构建一个 CRUD 应用程序。

我的代码是这样的http://jsfiddle.net/fpnna/1/

当我打开

时,我使用 apache 作为网络服务器
$locationProvider.html5mode(true);

然后得到

uncaught TypeError: Object #<Ic> has no method 'html5mode' from testApp 

当我单击“添加新”时,路径更改为“/new”但出现错误

404 The requested URL /new was not found on this server.

知道我哪里做错了。

我看了官方手册,还是没明白。

提前致谢。

最佳答案

你有几个问题,首先在 jsfiddle 中你不需要 body 标签,而且你有多个 body 标签。另外你的 fiddle 有两个 ng-apps,路由定义不正确(例如应该是/new),无效的 ng-view 结束标记,应该只有一个。您应该在 head 中包含没有换行的 javascript,最后它是 html5Mode,模式上带有大写 M,并且您的部分内容不存在于其 url 中,也没有定义为本地脚本。

我建议您使用 plunkr,因为它允许您添加其他本地文件,即 fiddle 中不存在的部分文件。

我已经解决了这个 plunkr 上的所有问题:http://plnkr.co/edit/A23Fxn9Ji02TGZ0jouZR?p=preview

angular.module('testApp', []).
config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true); // case is important
$routeProvider.
when("/", {
templateUrl: "list.html"
}).
when("/new", { // make sure and use absolute link to route
templateUrl: "edit.html"
})
})

function testCtrl($scope) {
$scope.persons = [{
name: "X"
}, {
name: "Y"
}, {
name: "Z"
}]
}

和 html:

<body ng-controller="testCtrl" >
<div class="main-nav"> <a href="new">Add Me</a>
</div>INDEX
<div >
<div ng-view>

</div>
</div>
</body>

请查看文档和教程,了解设置项目的基础知识。 http://docs.angularjs.org/guide/bootstrap

关于javascript - AngularJS 路由配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16462307/

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