javascript - 使用 Angular 指令中的 jQuery.matchHeight

标签 javascript jquery angularjs angularjs-directive angularjs-controller

我正在尝试使用 jQuery.matchHeight 将元素设置为相同的高度。我从 Angular 指令调用该函数

angular.module('myApp')
  .directive('matchHeight', ['$timeout', function ($timeout) {
    var linkFunction = function(scope, element) {
      $timeout(function() {
        angular.element(element).matchHeight();
      });
    };

  return {
    restrict: 'A',
    link: linkFunction
  };
}]);

matchHeight插件和jQuery包含在index.html中

<html>
  <head>
    all head stuff
  </head>
  <body>

    <div class="row usps">
      <div class="col-sm-4 usp-block" ng-repeat="block in content.marketing" match-height>
        <a href="{{block.link_url}}" class="thumbnail">
          // Rest of html
        </a>
      </div>
    </div>

    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/matchHeight/dist/jquery.matchHeight.js"></script>

    <script src="scripts/app.js"></script>
    <script src="scripts/directives/matchheight.js"></script>
  </body>
</html>

问题是尽管指令应用于元素,但高度并未设置。

最佳答案

jQuery.matchHeight plugin会将数组中的所有项目设置为该数组中最高元素的高度。

match-height 指令应用于单个元素。由于没有数组,因此未在元素上设置高度。

将指令移至 DOM 中的父元素并向子元素添加类 equal 即可得到设置高度所需的数组。

<div class="row usps" match-height>
  <div class="col-sm-4 usp-block equal" ng-repeat="block in content.marketing">
    <a href="{{block.link_url}}" class="thumbnail">
      // Rest of html
    </a>
  </div>
</div>

在服务中,我将 matchHeight 函数应用于具有类 equal 的所有元素

angular.module('myApp')
  .directive('matchHeight', ['$timeout', function ($timeout) {
    var linkFunction = function(scope, element) {
      $timeout(function() {
        angular.element(element).find('.equal').matchHeight();
      });
    };

    return {
      restrict: 'A',
      link: linkFunction
    };
}]);

关于javascript - 使用 Angular 指令中的 jQuery.matchHeight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40155223/

相关文章:

javascript - 如何判断隐藏字段何时更改

javascript - AngularJS:忽略 Content-Type 参数

javascript - 如何使用 AJAX 和 PHP 迭代将 JSON 数据保存到新的 JSON 文件?

javascript - 计算 0% 利息?

下拉列表中的 jquery 值选中禁用复选框

jquery - 使用 jquery 修改全局 css 声明

javascript - AngularJS 模块名称作为常量

angularjs - ng-map:单击时如何获取 Marker 对象?

javascript - 如何从unix时间戳中仅获取年份?

javascript - 如何从 import.io API 获取数据?