javascript 锚链接与角度路线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34233086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
anchor href linking with angular routes
提问by jola
I use angular routing between multiple views and try to populate a common breadcrumb list on each route change. This works fine except that the hyperlinks in the breadcrumb don't work.
我在多个视图之间使用角度路由,并尝试在每次路由更改时填充一个通用的面包屑列表。这工作正常,只是面包屑中的超链接不起作用。
Essentially I have a site with the following views:
基本上,我有一个具有以下视图的站点:
views/main.html
视图/main.html
views/page_a.html
视图/page_a.html
views/page_b.html
视图/page_b.html
and structure:
和结构:
main > page a > page b
主页 > 页面 a > 页面 b
$rootScope.$on('$routeChangeSuccess', function(scope, next, current) {
var thisView = next.loadedTemplateUrl;
if (!thisView) return;
var breadcrumb = jQuery('#breadCrumb'); //<ol> container
breadcrumb.empty();
if (thisView.indexOf('page_a') >= 0) {
breadcrumb.append('<li><a href="#/main">main</a></li>');
breadcrumb.append('<li class="active">page a</li>');
}
else if (thisView.indexOf('page_b') > 0) {
breadcrumb.append('<li><a href="#/main">main</a></li>');
breadcrumb.append('<li><a href="#/page_a">page a</a></li>');
breadcrumb.append('<li class="active">page b</li>');
}
});
unfortunately those hyperlinks doesn't go to the right place. I think I have tried all combinations of e.g. #/page_a, #/page_a.html, /views/page_a.html, ... but no luck. Feel this shouldn't be hard but it's getting late so I hope for some help. Thanks!
不幸的是,这些超链接没有放在正确的地方。我想我已经尝试了所有的组合,例如#/page_a、#/page_a.html、/views/page_a.html,...但没有运气。觉得这应该不难,但已经晚了,所以我希望得到一些帮助。谢谢!
EDIT
编辑
My routes are set up like:
我的路线设置如下:
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/page_a', {
templateUrl: 'views/page_a.html'
})
.when('/page_b', {
templateUrl: 'views/page_b.html'
})
.otherwise({
redirectTo: '/'
});
});
回答by jola
ok, a very stupid mistake and I wouldn't have needed to post this question at all..
Indeed href="#/page_a"
. It works just fine so this was just as easy as expected.
好吧,一个非常愚蠢的错误,我根本不需要发布这个问题..确实href="#/page_a"
。它工作得很好,所以这和预期的一样容易。