Reputation: 7475
I have the following scenario:
<div class="parent" style="height:100%">
<div class="left" style="float:left" dynamic-height element-id="{{$index}}">
</div>
<div class="right" style="float:left">
<div>Element 1</div>
<div>Element 2</div>
</div>
</div>
I'm trying that the left element will be at the same height as the .right or the .parent, i tried to add directive for that, with not much success, anyone have a better idea?
the directive:
.directive('dynamicHeight', function() {
return {
compile: function compile(tElement, tAttrs, transclude) {
return {
post: function postLink(scope, elem, attrs) {
var element = "#discussion-element-"+ attrs.elementId;
var myEl = angular.element( document.querySelector( element) );
var height = myEl[0].offsetHeight;
element.height(height);
}
}
}
};
});
Image that illustrate the issue:
Upvotes: 3
Views: 5904
Reputation: 34207
as commented, you can use css to get that. (e.g. https://css-tricks.com/fluid-width-equal-height-columns/)
you can add a $watch
inside the directive's scope to get a callback when the width & height
of the parent element changes. from there you can change the element height
html
<div class="container demo">
<div ng-controller="MyCtrl">
<div>
<button class="btn btn-primary" ng-click="isLarger = !isLarger">toggle parent size</button>
</div>
<hr>
<div class="parent" ng-class="{'larger':isLarger}">
<div class="child" dynamic-height>{{message}}</div>
</div>
</div>
</div>
js
var app = angular.module('myApp', []);
app.controller('MyCtrl', function ($scope) {
$scope.message = 'hello';
});
app.directive('dynamicHeight', function () {
return {
restrict: 'A',
link: function (scope, elm, attrs) {
var parent = elm.parent();
scope.$watch(function () {
return {
height: parent.prop('offsetHeight'),
width: parent.prop('offsetWidth')
};
}, function (size) {
// Here you have `size = {height: ... , width: ... }`
// you can manipulate the element with this data. for example:
// elm.css('min-width', size.width + 'px');
// elm.css('min-height', size.height + 'px');
// -- note that this will make the responsiveness of the ui a bit more challanging
}, true);
}
};
});
Upvotes: 1
Reputation: 338
Try removing the float: left
style and applying these styles instead:
.parent {
display: table;
}
.parent > * {
display: table-cell;
}
Upvotes: 2