简体   繁体   English

Angular SPA中不需要的页面刷新

[英]Unwanted page refreshing in Angular SPA

I have Angular routing setup using HTML5 mode like so: 我有使用HTML5模式的Angular路由设置,如下所示:

app.config($routeProvider, $locationProvider){

  $locationProvider.html5Mode(true);

  $routeProvider.when("/", {
    templateUrl: "/views/login.html"
  }).when("/dashboard", {
    templateUrl: "/views/dashboard.html"
  })

});

Now I move between routes with simple links like this: 现在,我在带有简单链接的路线之间移动,如下所示:

<a href="/dashboard"></a>

This works fine, except I seem to get a full page reload when I click the link. 这工作正常,但单击链接后似乎重新加载了整页。 Angular isn't meant to do this so I'm not sure what's going on. Angular并不是要这样做,所以我不确定发生了什么。

It may be worth noting that in reality my script is substantially more complicated than this, but having debugged everything, I can't see anything wrong other than the fact that I recently switched to the HTML5 mode. 可能值得注意的是,实际上我的脚本比这要复杂得多,但是调试了所有内容之后,除了最近切换到HTML5模式外,我看不到其他任何错误。

How do I stop the page reload? 如何停止页面重新加载?

Try using ui-sref directive. 尝试使用ui-sref指令。 It will use $state service to load a page. 它将使用$state服务加载页面。

<a ui-sref="yourroute">Link</a>

You have to configure your routes with $stateProvider to use route names. 您必须使用$stateProvider配置路由以使用路由名称。

If you're using Angular routing your link should start with a hash to prevent the browser from navigating away from the page. 如果您使用的是Angular路由,则链接应以哈希开头,以防止浏览器离开页面。 Have you tried: 你有没有尝试过:

<a href="#/dashboard"></a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM