Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular框架中当前页面跳转大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我们在使用 $routeProvider后,想在当前页面跳转连接,往往得不到正确的跳转

例如:
test.html

<a href="#faq-1">Question 1</a>
<a href="#faq-2">Question 2</a>
<a href="#faq-3">Question 3</a>

<h3 id="faq-1">Question 1</h3>
<h3 id="faq-2">Question 2</h3>
<h3 id="fa1-3">Question 3</h3>

由于 我们可能设置了 $routeProvider
app.js

var app = angular.module('angularjs-starter',[]);

app.config(function($routeProvider) { 
  $routeProvider.when('/test',{
    controller: 'TESTCtrl',templateUrl: 'test.html'
  })
  .when('/weee',{
    controller: 'WeeeCtrl',templateUrl: 'weee.html'
  })
  .otherwise({
     redirectTo: '/test'
   });
});

解决这个页面跳转,有很多种解决办法:

1、利用 ng-click 替换原来的 href,并添加@R_262_11258@Controllerx响应

2、利用 $LOCATIOn.hash()

app.run(function($rootScope,$LOCATIOn,$anchorScroll) {
  //when the route is changed scroll to the proper element.
  $rootScope.$on('$routeChangesuccess',function(newRoute,oldRoutE) {
    if($LOCATIOn.hash()) $anchorScroll();  
  });
});
<a href="#/test#foo">Test/Foo</a>

但是会刷新的整个页面

3、在a标签添加 target=”_self”

<a href="#faq-1" target="_self">Question 1</a>

这个方法简单有效!

具体方法可以参

http://stackoverflow.com/questions/14712223/how-to-handle-anchor-hash-linking-in-angularjs

大佬总结

以上是大佬教程为你收集整理的Angular框架中当前页面跳转全部内容,希望文章能够帮你解决Angular框架中当前页面跳转所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。