External Links in Angular App

老子叫甜甜 提交于 2019-12-06 04:02:43

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

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).

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.

Another option that may work for you is to change the $rootElement: the link interception only occurs within the element that is declared as the ng-app.

In my case, I switched from <body ng-app="myApp"> to <section id="my-rich-area" ng-app="myApp">.

This worked for me as I didn't have any same-domain, non-angular links within that section, ymmv.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!