VSO
VSO

Reputation: 12646

Get clientHeight of div using Angular?

I need to get the clientHeight of a div? How do I do this in Angular? (I don't care if it's bad practice at this point)

I have tried a thousand things, some of which can be seen in this plunker. The code below works outside of a controller, but not for clientHeight:

    $scope.myElem = angular.element(document.getElementById('testDiv')); 
    $scope.myAttr = $scope.myElem.attr('name'); 

I don't know how to get the attributes from within a directive. All examples seem to only use link with named attributes. I won't be setting the clientHeight, I just need to return it.

Upvotes: 0

Views: 2425

Answers (1)

Phil
Phil

Reputation: 164798

Using a directive would be the easiest way. The DOM is fully realised in the link (post link) function so element properties should be available.

For example

.directive('logClientHeight', function($log) {
    return {
        link: function(scope, element) {
            $log.debug(element.prop('clientHeight'));
        }
    }
});

Plunker


Note that for the element to have typical element properties, it should be a typical element.

Thanks Eric Martinez for pointing that out.

Upvotes: 6

Related Questions