【问题标题】:$anchorScroll is working on second click, when I have called in other submit function当我调用其他提交函数时,$anchorScroll 正在第二次点击
【发布时间】:2016-02-18 09:27:00
【问题描述】:

我仍然面临这个问题。我的问题有点不同

我在其他事件中调用我的 $anchorScroll 函数。提交表单时。它正在第二次单击与上述相同。我也尝试过较早的anwsered解决方案,但没有运气:(

 $scope.scrollTo = function(element) {
    var old = $location.hash();
    $location.hash(element);
    $anchorScroll();
    $( 'html, body').animate({
        scrollTop: $(element).offset().top
    }, 1000);
    $location.hash(old);
};

我在另一个提交表单的函数中调用它。

 $scope.saveProfile = function (profile) {
 $log.debug("now the profile is " + JSON.stringify(profile));
 dataFactory.persistChange("profiles",profile,$scope,postSuccessUpdate);
  $scope.scrollTo( "#error ");
}

这是我的锚

 <div ng-class="statusDisplayClass" ng-show="messages" id="error">
       Here it will be displayed
      </div>

请帮助我。我缺乏的地方。

【问题讨论】:

    标签: javascript angularjs nested location


    【解决方案1】:

    你能不能把你的$scope.scrollTo( "#error ");改成$location.hash('error'); $anchorScroll();

    它应该如下所示:

     $scope.saveProfile = function (profile) {
       $log.debug("now the profile is " + JSON.stringify(profile));
         dataFactory.persistChange("profiles",profile,$scope,postSuccessUpdate);
          $location.hash('error');
          $anchorScroll();
     }
    

    我不确定这是否有效,因为在我的环境中它可以正常工作。

    【讨论】:

    • 感谢您的快速回复@gracia。
    • 在我的场景中,我在另一个函数中调用这个 $scope.scrollTO('#error'),它在表单提交时触发。所以,它首先渲染 saveProfile 函数并渲染整个 DOM,我面临这个问题。此外,我也尝试过使用 $routeParams 和其他路由插件,但没有成功。
    • 有人对我的查询有任何进一步的解决方案吗?请帮助我,因为我是 angularjs 的新手 :)
    • 您是否尝试阅读此内容:docs.angularjs.org/api/ng/service/$anchorScroll
    • 是的,我已经尝试了所有这些方法。我的场景是这样的。我有一个名为“saveProfile”的函数,它正在获取响应并抛出错误或成功消息。我试图调用这个消息,当它得到响应但它仍然在第二次点击时工作。我不知道这里发生了什么。我也试图停止传播,但没有运气。再次感谢您的回复。我会弄清楚并在此告知问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2017-07-28
    • 2018-05-25
    • 1970-01-01
    • 2019-09-28
    相关资源
    最近更新 更多