Angularjs根据父元素宽度调整宽度

时间:2013-12-24 07:17:55

标签: javascript css angularjs

我正在使用AngularJS& Bootstrap,具有以下结构:

<parent-div>
  <flexible-width-component 
            class="pull-left" 
            style="display: inline-block; min-width: 700px;">Data grid with many columns
  </flexible-width-component>
  <fixed-width-component 
            class="pull-right" 
            style="width:400px; display: inline-block">
  </fixed-width-component>
</parent-div>

我希望我的flexible-width-component拉伸能够自动填补自身与fixed-width-component之间的空白,因为任何分辨率都超过1200px。 两个组件都需要彼此相邻显示。

非常感谢任何建议!

2 个答案:

答案 0 :(得分:15)

您可以获取父容器offsetWidth并从中减去固定宽度:

var example = angular.module('exmp', []);

example.directive('flexibleWidth', function() {
    return function(scope, element, attr) {

      // Get parent elmenets width and subtract fixed width
      element.css({ 
        width: element.parent()[0].offsetWidth - 400 + 'px' 
      });

    };
});

这是 a demo

答案 1 :(得分:1)

我知道回答这个问题已经很晚了,但它可能对其他人有所帮助。

工作jsfiddle

<div class="parent border">
  <div class="fixed-component border">
    Fixed Component
  </div>
  <div class="flexible-component border">
    flexible component
  </div>
</div>


<style>
.parent {
  height: 100px;
  display: flex;
}

.flexible-component {
  flex: 1;
}

.fixed-component {
  width: 100px;
}
.border {
  border: 1px solid black;
}
</style>
相关问题