简体   繁体   English

Angular-jQuery:将jQuery放在单独的文件中并得到错误“无法读取未定义的属性” top”

[英]Angular-jQuery : Putting jQuery in a separate file and getting error 'Cannot read property 'top' of undefined'

I have a project base on AngularJS but I am trying to use a jQuery code in order to have a sticky sidebar working, when I put my code on Plunkr, everything works properly , but I do not know why isn't working on my project, and actually I have 2 problems, that's the 1st one. 我有一个基于AngularJS的项目,但我尝试使用jQuery代码以使粘性侧栏正常工作,当我将代码放在Plunkr上时,一切正常 ,但是我不知道为什么在我的项目上不工作,实际上我有两个问题,这是第一个问题。

The 2nd one: if I put my jQuery code in the same html file within <script></script> tags everything works fine. 第二个问题:如果我将jQuery代码放在<script></script>标记内的同一html文件中,则一切正常。 The problem is when I move that code to a separate file, then the error Cannot read property 'top' of undefined comes up to the browser console. 问题是当我将该代码移到一个单独的文件时,浏览器控制台出现错误Cannot read property 'top' of undefined

This is the HTML: 这是HTML:

  <div class="col-md-3" id="sidebar">

    <div>

      <div class="panel">
        <div class="panel-group">
         ...
            </accordion-group>
          </accordion>
        </div>
      </div>

    </div>

  </div>

and here is the jQUery: 这是jQUery:

$(function() {
  var offset = $('#sidebar').offset(),
      topPadding = 85;
  $(window).scroll(function() {
    if ($(window).scrollTop() > offset.top) {
      $('#sidebar').stop().animate({
        marginTop: $(window).scrollTop() - offset.top + topPadding
      });
    } else {
      $('#sidebar').stop().animate({
        marginTop: 50
      });
    }
  });
});

Remember to check my Plunkr 记得检查我的Plunkr

And also remember that the code works properly only when you include it on the HTML file, but once you put that jQuery code in a new file, then the error comes up. 还请记住,只有将代码包含在HTML文件中,该代码才能正常工作,但是一旦将jQuery代码放入新文件中,就会出现错误。

Make sure you are calling the jquery file before you are trying to use the jquery function. 在尝试使用jquery函数之前,请确保已调用jquery文件。 Are you able to go into your sources in Chrome and see the jquery file? 您是否可以在Chrome中查看源代码并查看jquery文件? If not, something is wrong with your import. 如果不是,则您的导入有问题。

Actually I just realized what is going on, all you need to do is set a angular.element(document).ready(function(){...} , the reason why the error appears is because the DOM is not completely load. And at the end I did an Angular Directive for better approach 实际上我只是意识到发生了什么,您需要做的只是设置一个angular.element(document).ready(function(){...} ,出现此错误的原因是因为DOM尚未完全加载。最后,我做了一个Angular Directive以获得更好的方法

So: 所以:

angular.module('myApp', [])
.directive('sticky', function($document) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      angular.element(document).ready(function() {
        var offset = element.offset(),
            topPadding = 85;
        $document.scroll(function() {
          if ($document.scrollTop() > offset.top) {
            element.stop().animate({
              marginTop: $document.scrollTop() - offset.top + topPadding
            });
          } else {
            element.stop().animate({
              marginTop: 0
            });
          };
        });
      });
    }
  };
});

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

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