简体   繁体   English

使用angularJS重定向页面

[英]Redirection page with angularJS

I use an angularJS script with a html page. 我在HTML页面上使用angularJS脚本。 I use this library : https://github.com/oblador/angular-scroll 我使用这个库: https : //github.com/oblador/angular-scroll

I use a script provided by this library to have a scroll spy in my url. 我使用该库提供的脚本在URL中包含滚动监视功能。 It works well but if I have a link to redirect to another page and it doesn't reload the page. 它运作良好,但是如果我有一个重定向到另一个页面的链接并且它不会重新加载该页面。 The url changed but not the content. 网址已更改,但内容未更改。 If I deleted the script the link works. 如果我删除了脚本,则链接有效。

this is the script : 这是脚本:

var myApp = angular.module('app_example', ['duScroll'])
.config(function($locationProvider) {
  $locationProvider.html5Mode({
    enabled: true,
    requireBase: false
  })
}).
run(function($rootScope, $location) {
  $rootScope.$on('duScrollspy:becameActive', function($event, $element){
    //Automaticly update location
    var hash = $element.prop('hash');
    if (hash) {
      $location.hash(hash.substr(1)).replace();
      $rootScope.$apply();
    }
  });
});

This is plunker with the script : http://plnkr.co/edit/emrTAvTPMKbLUNYi40Xq?p=preview 这个脚本的使用很简单: http ://plnkr.co/edit/emrTAvTPMKbLUNYi40Xq?p=preview

And the plunker without : http://plnkr.co/edit/xfsAWsLADkW6F98exC4P?p=preview 和没有的插件: http ://plnkr.co/edit/xfsAWsLADkW6F98exC4P?p=preview

You should assign "true" to "html5Mode.requireBase" 您应该将“ true”分配给“ html5Mode.requireBase”

relative link 相对链接

In fact, we don't have to use $location , history is enough :) 实际上,我们不必使用$location ,历史就足够了:)

The new script : 新脚本:

.run(function($rootScope, $location) {
    if(!history || !history.replaceState) {
      return;
    }
    $rootScope.$on('duScrollspy:becameActive', function($event, $element){
      //Automaticly update location
      var hash = $element.prop('hash');
      if (hash) {
        history.replaceState(null, null, hash);
      }
    });
  });

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

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