角度路线-URL中的额外编号 [英] Angular Route - Extra # in URL
问题描述
学习一些Angular-我被路由困住了
Learning some Angular - and I'm stuck on routing
这是我的角度配置
var meanApp = angular.module('carz', ['ngRoute']);
meanApp.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.html',
controller: 'mainCtrl'
})
.when('/red', {
templateUrl: 'red.html',
controller: 'redCtrl'
});
});
这是我的链接
<a href="#">Home</a>
<a href="#red">Red</a>
当我加载节点应用程序时,我将被定向到
When I load up my node app I am directed to
http://localhost:8080/#!/
然后我的角度控制器在ng-view
标签中按预期工作
And I get my angular controller working as expected within the ng-view
tags
但是我不能使用上面的链接从一个控制器切换到另一个控制器.
But I cannot switch from one controller to the other using the links above.
如果我选择红色标记,则我的网址会添加#号变种
http://localhost:8080/#!/#red
请注意,如果我手动更改为
Note if I manually change to
http://localhost:8080/#!/red
我的控制器发生了变化并且可以正常工作,所以为什么我要获得额外的#
My controller changes and it works so why am I getting the extra #
感谢您的帮助
推荐答案
自AngularJS 1.6起,路由方面发生了重大变化:
Since AngularJS 1.6 there is a breaking change in routing:
$ location hash-bang URL的哈希前缀已从空字符串"更改为bang!".
The hash-prefix for $location hash-bang URLs has changed from the empty string "" to the bang "!".
(请参见 https://github.com/angular/angular .js/blob/master/CHANGELOG.md )
解决方案:
要么开始使用#!代替 # 或将$ locationProvider设置为仅使用#接受,如下所示:
either start using #! Instead of # OR set up $locationProvider to accept just using #, like this:
appModule.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}]);
这篇关于角度路线-URL中的额外编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!