angularjs 1.6.0(最新的)路线不工作

Awa*_*ine 97 angularjs angular-routing ngroute angularjs-ng-route angularjs-1.6

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

我有一个我正在研究的基本项目,但即使我到目前为止所做的一切看起来都是正确的,但这些路线似乎并不起作用.

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

<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>
Run Code Online (Sandbox Code Playgroud)

这是我的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: '/' });
}]);
Run Code Online (Sandbox Code Playgroud)

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

HTTP://本地主机:8000 /管理员#/

当我点击Add quote按钮时,我得到了这个:

HTTP://本地主机:8000 /管理#/#%2Fadd引号

这可能是什么问题?感谢帮助

geo*_*awg 168

只需#!在href中使用hashbang :

 <a href="#!/add-quote">Add Quote</a>
Run Code Online (Sandbox Code Playgroud)

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

如果您确实不想使用hash-prefix,则可以通过向应用程序添加配置块来恢复以前的行为:

appModule.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅


很抱歉让我的高马,但......这是如何发布的?这是一个巨大的,破坏bug.- @MiloTheGreat

由于参考规范已经正式弃用#15715,因此应该恢复#14202的重大变化

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

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

  • 很抱歉让我的高马,但......这是如何发布的?这是一个巨大的,破坏bug. (16认同)
  • @MiloTheGreat随时将您的反馈留给AngularJS团队 - https://github.com/angular/angular.js/issues/15715 (2认同)

v-t*_*tec 39

只需包含!以下内容href:

<body>
    <a href="#!/add-quote">Add Quote</a>
    <div ng-view ></div>
</body>
Run Code Online (Sandbox Code Playgroud)

  • 哇浪费了几个小时后,关于路由的弃用教程..我所缺少的只是'!'.谢谢! (7认同)

lix*_*onn 5

我无法在1.6.4中使用路由,因此我决定使用angular 1.5.11并且路由工作正常但我需要在when(..)函数中定义所有路由,尾随"/"

如果坚持使用较旧版本的角度是你的选择,那么考虑它,因为它可以节省你的神经...

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

app.config(function($routeProvider) {
$routeProvider
.when("/layoutandviewbox", {
    templateUrl : "views/layout-and-viewbox.html"
})
.when("/basicshapes", {
    templateUrl : "views/basic-shapes.html"
})
.when("/advancedshapes", {
    templateUrl : "views/advanced-shapes.html"
})
.when("/groups", {
    templateUrl : "views/groups.html"
})
.when("/transformations", {
    templateUrl : "views/transformations.html"
})
.when("/effects", {
    templateUrl : "views/effects.html"
})
.when("/", {
    templateUrl : "views/basic-shapes.html"
});
});
Run Code Online (Sandbox Code Playgroud)