加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular框架中当前页面跳转

发布时间:2020-12-17 10:05:19 所属栏目:安全 来源:网络整理
导读:我们在使用 $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"

我们在使用 $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,并添加相应的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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读