javascript - 让 angular-masonry 工作它说它没有方法 'imagesLoaded'

标签 javascript angularjs angularjs-directive jquery-masonry

我正在尝试获取 angular-masonry通过 passy 使用无限滚动指令,但我遇到了一些问题。我在 plnkr here 中做这件事.它在控制台中显示为错误,TypeError: Object [object Object] has no method 'imagesLoaded'。

这是我的html

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@*" data-semver="1.2.13" src="http://code.angularjs.org/1.2.13/angular.js"></script>
    <script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script data-require="masonry@*" data-semver="2.1.0-7" src="//cdnjs.cloudflare.com/ajax/libs/masonry/2.1.07/jquery.masonry.min.js"></script>
    <script data-require="imagesloaded@*" data-semver="v3.0.2" src="http://desandro.github.io/imagesloaded/imagesloaded.pkgd.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="angular-masonry.js"></script>
    <script src="myApp.js"></script>
  </head>

  <body ng-app="myApp" ng-controller="DemoController">
    <div masonry="">
      <img class="repeated-img masonry-brick" ng-repeat="image in images" ng-src="http://placehold.it/225x{{image.height}}/{{image.color.bg}}/{{image.color.text}}&text={{image.num}}" />
    </div>
    <div scroll-trigger="loadMore()" threshold="100">Trigger element</div>
  </body>

</html>

还有我的无限滚动指令和应用模块

var myApp = angular.module('myApp', ['wu.masonry']);
myApp.controller('DemoController', function($scope) {
  $scope.images = [
    {num: 1, height:400}, {num: 2, height:250}, 
    {num: 3, height:225}, {num: 4, height:200}, 
    {num: 5, height:200}, {num: 6, height:225}, 
    {num: 7, height:300}, {num: 8, height:250}, 
    {num: 9, height:275}, {num: 10, height:350}, 
    {num: 11, height:450}, {num: 12, height:275}, 
    {num: 13, height:225}, {num: 14, height:250}, 
    {num: 15, height:250}, {num: 16, height:225}]

  $scope.loadMore = function() {
    var last = $scope.images[$scope.images.length - 1].num;
    var heights = [200, 225, 250, 275, 300, 350, 400, 450]
    var colors = ["859994", "51C0C4", "C3D9C6", "EBE2C7", "F5E6D3"]
    var textColor = "ffffff";
    for(var i = 1; i <= 8; i++) {
      var randColor = colors[Math.floor(Math.random()*colors.length)];
      $scope.images.push({num: last + i, height: (heights[Math.floor(Math.random()*heights.length)]), color: {bg: randColor, text: textColor}});
    }
  };
});

myApp.directive('scrollTrigger', function($window) {
    return {
        link : function(scope, element, attrs) {
            var offset = parseInt(attrs.threshold) || 0;
            var e = jQuery(element[0]);
            var doc = jQuery(document);
            angular.element(document).bind('scroll', function() {
                if (doc.scrollTop() + $window.innerHeight + offset > e.offset().top) {
                    scope.$apply(attrs.scrollTrigger);
                }
            });
        }
    };
});

谢谢,抱歉,但我是 Angular 的新手。

最佳答案

你应该在 angular 之前加载 jQuery,我也使用了较新版本的 masonry (3.1.2),因为它似乎加载的图像依赖于较新的版本:

<script src="//code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/masonry/3.1.2/masonry.pkgd.min.js"></script>
<script src="//desandro.github.io/imagesloaded/imagesloaded.pkgd.min.js"></script>
<script src="//code.angularjs.org/1.2.13/angular.js"></script>

来自 angular.element(jqLite) docs :

If jQuery is available, angular.element is an alias for the jQuery function. If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or "jqLite."

这是源代码(https://github.com/angular/angular.js/blob/v1.2.13/src/Angular.js#L1257):

function bindJQuery() {
  // bind to jQuery if present;
  jQuery = window.jQuery;
  // reset to jQuery or default to us.
  if (jQuery) {
    jqLite = jQuery;
    extend(jQuery.fn, {
      scope: JQLitePrototype.scope,
      isolateScope: JQLitePrototype.isolateScope,
      controller: JQLitePrototype.controller,
      injector: JQLitePrototype.injector,
      inheritedData: JQLitePrototype.inheritedData
    });
    // Method signature:
    //     jqLitePatchJQueryRemove(name, dispatchThis, filterElems, getterIfNoArguments)
    jqLitePatchJQueryRemove('remove', true, true, false);
    jqLitePatchJQueryRemove('empty', false, false, false);
    jqLitePatchJQueryRemove('html', false, false, true);
  } else {
    jqLite = JQLite;
  }
  angular.element = jqLite;
}

关于javascript - 让 angular-masonry 工作它说它没有方法 'imagesLoaded',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21962972/

相关文章:

javascript - 如何更改与源关联的图像

javascript - DroidScript - TCP 随时接收

javascript - 将外部 javascript 内容显示为 html 页面中的文本

javascript - 使用 AngularJS 编辑数组中的非字符串对象

java - 为什么我需要在 tomcat 应用程序上启用 CORS

javascript - AngularJS 指令 : put a call function in an attribute, 不包含另一个属性

javascript - AngularJS 在 Controller 中注入(inject)模块

javascript - 无法操作 $.post 中的数据

javascript - Angular Protractor : Execute Scripts in Browser Context

javascript - 从 Controller Angularjs 调用链接函数