angularjs 1.6.0(现在最新)路由不起作用 [英] angularjs 1.6.0 (latest now) routes not working

查看:29
本文介绍了angularjs 1.6.0(现在最新)路由不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在 Stackoverflow 上看到这个问题,但没有.显然我是唯一一个在我看来很常见的问题.

I was expecting to see this question on Stackoverflow but didn't. Apparently I'm the only one having this problem that seems to me to be very common.

我有一个基本项目正在进行,但即使到目前为止我所做的一切似乎都是正确的,但路线似乎不起作用.

I have a basic project I am working on but the routes don't seem to work even though everything I've done so far seems to be right.

我的 index.html 文件中有这段 html:

I have this piece of html in my index.html file:

<html>
<head ng-app="myApp"> 
    <title>New project</title>
    <script src="https://code.angularjs.org/1.6.0/angular.min.js"></script>
    <script src="https://code.angularjs.org/1.6.0/angular-route.min.js"></script>

    <script src="app.js"></script>
</head>
<body>
    <a href="#/add-quote">Add Quote</a>
    <div ng-view ></div>
</body>
</html>

这是我的app.js:

var app = angular.module('myApp', ['ngRoute']);


app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/add-quote', {
            templateUrl: 'views/add_quote.html',
            controller: 'QuoteCtrl'
        })
        .otherwise({ redirectTo: '/' });
}]);

现在,当我刚刚访问该页面时,这是我在 url 中得到的内容:

Now when I just visit the page, here is what I get in the url:

http://localhost:8000/admin#!/

当我点击添加引用按钮时,我得到了这个:

and when I click on the Add quote button, I get this:

http://localhost:8000/admin#!/#%2Fadd-quote

这里有什么问题?感谢帮助

What can be the problem here? Thanks for help

推荐答案

只需在 href 中使用 hashbang #!:

Simply use hashbang #! in the href:

 <a href="#!/add-quote">Add Quote</a>


由于 aa077e8,用于 $location 哈希的默认哈希前缀-bang URLs 已从空字符串 ('') 更改为 bang ('!').


Due to aa077e8, the default hash-prefix used for $location hash-bang URLs has changed from the empty string ('') to the bang ('!').

如果你真的不想要哈希前缀,那么你可以通过向你的应用程序添加一个配置块来恢复以前的行为:

If you actually want to have no hash-prefix, then you can restore the previous behavior by adding a configuration block to your application:

appModule.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);

有关详细信息,请参阅

很抱歉让我上马,但是...这是如何发布的?这是一个巨大的、破坏性的错误.— @MiloTheGreat

#14202 的重大更改应该恢复,因为参考规范已经正式弃用 #15715

我将关闭此问题,因为我们还没有收到任何反馈.如果您能提供新的反馈,请随时重新打开此问题.

——https://github.com/angular/angular.js/issues/15715#issuecomment-281785369

这篇关于angularjs 1.6.0(现在最新)路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆