AngularJS应用程序使用HTML5模式在锚定标签中链接/路由

前端之家收集整理的这篇文章主要介绍了AngularJS应用程序使用HTML5模式在锚定标签中链接/路由前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何链接到锚A标签中的其他AngularJS应用程序位置?我使用 HTML5无哈希模式,并希望避免让页面实际重新加载.

例如,在非HTML5模式下,我会这样做:

<a href='#/path'>Link</a>

在HTML5模式下,我可以这样做:

<a href='/path'>Link</a>

但这实际上导致浏览器重新加载新的URL.我也尝试过使用ng-href和/#/ path语法,但是似乎没有任何一种工作方式.

如何从锚标签正确链接

更新:

似乎这样可能没有使用$位置,你只需要保持相同的基本链接. From the docs

When you use HTML5 history API mode,you will need different links in different browsers,but all you have to do is specify regular URL links,such as: <a href=”/some?foo=bar”>link</a>

When a user clicks on this link,

  • In a legacy browser,the URL changes to /index.html#!/some?foo=bar
  • In a modern browser,the URL changes to /some?foo=bar

In cases like the following,links are not rewritten; instead,the browser will perform a full page reload to the original link.

  • Links that contain target element. Example: <a href=”/ext/link?a=b” target=”_self”>link</a>
  • Absolute links that go to a different domain. Example: <a href=”http://angularjs.org/”></a>
  • Links starting with ‘/’ that lead to a different base path when base is defined. Example: <a href=”/not-my-base/link”>link</a>

旧:

您应该使用$location service.将其注入控制器并将其放在$范围(或方便方法)中:

function MainCtrl($scope,$location){
  $scope.goto = function(path){
    $location.path(path);
  }
}
<a ng-click="goto('/path')">Link</a>
原文链接:https://www.f2er.com/angularjs/142353.html

猜你在找的Angularjs相关文章