Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – $location.path将哈希更改为#&.hash不能正常工作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想重定向到另一个页面&焦点应该放在一些带有id的DIV上,让我们说’some-div-id’.
我试过跟随
$LOCATIOn.path('/' + $scope.config_path.school + '/' +
        $routeParams@L_635_4@meUrl + '/#some-div-id')

这工作正常,但它首先将#改为#
喜欢

/%23some-div-id  #If '#' is not already present in the URL
/%23some-div-id#some-div-id #If '#' is laready present in the URL
/#some-div-id

我也试过以下

$LOCATIOn.path('/' + $scope.config_path.school + '/' +
        $routeParams@L_635_4@meUrl + '/').hash('some-div-id')

它正在创建一个正确的URL但不向下滚动到ID为some-div-id的DIV

EDITED

app.run(function($rootScope,$LOCATIOn,$anchorScroll,$routeParams) {
  $rootScope.$on('$routeChangesuccess',function(newRoute,oldRoutE) {
    setTimeout(function() {
      if ($LOCATIOn.hash()) {
        $anchorScroll($LOCATIOn.hash());
      }
    });
  });
})

app.controller('MainCntrl',function($scope,$routeParams) {
});

我也试过$LOCATIOn.path& $LOCATIOn.url

尝试使用$LOCATIOn.url而不是$LOCATIOn.path.

根据文档,$location.path仅更改路径,而$location.url更改路径,搜索和哈希.

所以代码就像

$scope.goto = function() {
    $LOCATIOn.url('pageone#one');
};

pageone是状态的URL,#one是ID

另外,为了在直接访问带有ID的URL时使用它,请使用$anchorScroll.在$stateChangesuccess事件中,检查weather $LOCATIOn.hash是否存在.如果存在,则调用$anchorScroll.代码看起来像

.run(function($rootScope,$timeout) {
    $rootScope.$on('$stateChangesuccess',function(event,toState,toParams,fromState,fromParams) {
            $timeout(function() {
                if ($LOCATIOn.hash()) {
                $anchorScroll();
               }
          });
    });
})

示例 – http://plnkr.co/edit/4kJjiMJImNzwLjRriiVR?p=preview(用于查看URL检查http://run.plnkr.co/plunks/4kJjiMJImNzwLjRriiVR/#/pageone中的更改)

大佬总结

以上是大佬教程为你收集整理的angularjs – $location.path将哈希更改为#&.hash不能正常工作全部内容,希望文章能够帮你解决angularjs – $location.path将哈希更改为#&.hash不能正常工作所遇到的程序开发问题。

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

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