问题描述
我在角结合到现有的Rails应用的单个页面的过程。
I'm in the process of incorporating Angular into a single page of an existing rails app.
一切都与路由在页面中使用可以正常使用以下
Everything is working perfectly with the routing within the page using the following
app.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/services/:id', {
templateUrl: "/javascripts/angular/templates/service_ui/service.html",
controller: "ServiceCtrl"
})
$locationProvider.html5Mode(true);
});
不过,我想对于那些不相关的链接角维持正常功能。例如,我们有许多在头链接别处,目前正在由角路由器抓链接
However, I'd like to maintain normal functionality for links that are not related to Angular. For example, we have a number of links in the header that link elsewhere that are now being caught by the angular router.
我发现一些潜在的解决方案:
I've found some potential solutions at: https://groups.google.com/forum/?fromgroups#!topic/angular/basidvjscRk
但似乎没有work..and目标=_自我法的基本路径方法相当突兀。有没有更好的办法让那些不处于配置功能指定角忽略路线?
But the base path method doesnt seem to work..and the target="_self" method is rather obtrusive. Is there a better way to let angular ignore routes that aren't specified in the config function?
我知道有一个。否则()方法,但同样这似乎是一个黑客。我缺少的东西吗?
I know there is an .otherwise() method but again this seems like a hack. Am I missing something?
非常感谢!
推荐答案
我们有一个比较,它们的大部分环节的传统的Web应用程序触发整个页面重载;极少数链接通过角的路由系统。我们的模块定义之后,我们有以下几点:
We have a relatively "traditional" web application in that most of the links trigger full page reloads; very few links go through Angular's routing system. Right after our module definition, we have the following:
app.run(function($location, $rootElement) {
$rootElement.off('click');
});
这将停止built-in点击拦截的那个角使用操控的URL,比如当你点击一个链接;美中不足的是,你现在必须 $位置
手动,只要你想使用角的到的做它的URL魔力(如通过 ngClick
和函数,操纵 $位置
相应地)。
This stops the built-in interception of clicks that Angular uses to manipulate the URL and such when you click on a link; the catch is that you now have to use $location
manually whenever you want Angular to do its URL magic (e.g. via an ngClick
and a function that manipulates $location
accordingly).
您可以考虑使用 $ rootElement.off
加上一个特殊的指令,或配置功能重新安装上,你发现含有一定URL链接片段这种行为。
You may consider using $rootElement.off
combined with a special directive or configuration function that re-installs this behavior on links that you detect contain a certain URL fragment.
这篇关于在角应用外部链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!