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

查看:109
本文介绍了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#!/

,当我单击Add quote按钮时,我得到了:

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 URL已从空字符串('')更改为爆炸('!').


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('');
}]);

有关更多信息,请参见

  • AngularJS GitHub Pull #14202 Changed default hashPrefix to '!'
  • AngularJS Guide - Migration - aa0077e8

很抱歉让我骑高马,但是...这是怎么被释放的?这是一个巨大的,令人讨厌的错误. — @MiloTheGreat

由于参考规范已经正式弃用#15715,因此应还原#14202的重大更改

我将结束此问题,因为我们没有任何反馈.如果您可以提供新的反馈意见,请随时重新打开此问题.

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

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

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