6

I have DOM element,with id={{child.id}}container

 <div id={{child.id}}container layout="column" flex layout-align="start center" class='container' dragula='"first-bag"' ng-init="vm.getAssociatedWorkItems(child.id); vm.getElementHeight(child.id+'container');">
    <div layout="column" class="md-whiteframe-5dp capitalize itemsInList" ng-repeat="item in vm.items | filter:search" id={{item.id}}child>
        <div class="workItemName">{{vm.getWorkItemName(item.metadata)}}</div>
        <div class="workItemDescription">{{vm.getWorkItemDescription(item.metadata)}}</div>
    </div>
</div>

I want to get height of this element, and according to height deside to list items in ng-repeat or not. How to get DOMs height in angularjs?

5
  • Why should it impact the use of ng-repeat or not ? Commented Oct 6, 2016 at 8:51
  • I height is small I don't want to show items in ng-repeat. Commented Oct 6, 2016 at 8:52
  • 1
    you can use 'dir-paginate' an angular js pagination plugin.. based on your count it will display fixed number of records. after that pagination will come... Commented Oct 6, 2016 at 8:56
  • But why ? Is there a significant reason to not use ng-repeat ? Commented Oct 6, 2016 at 9:05
  • dir paginate is extended form of ng-repeat with pagination.. Commented Oct 6, 2016 at 9:21

3 Answers 3

14

Use angular built-int jqlite with angular.element():

angular.element(".myDiv")[0].offsetHeight;

Beware: don't pollute your controller with DOM manipulation. It's a bad practise and you should do this kind of operations in directives.

Edit 1

OP used this mid way after my suggestion:

var element = angular.element(document.querySelector('#id')); 
var height = element[0].offsetHeight;
Sign up to request clarification or add additional context in comments.

3 Comments

when I use : var x = angular.element('#'+id+'container'); console.log(x); I receive: j…y.fn.init {context: document, selector: "#165container"}. Cannot height height from there
Try with the standard javascript way: var element = angular.element(document.querySelector('.ass')); and then element[0].offsetHeight
@Luxor001 will this give us the height of div in ng-repeat also ? If the limit is set to 2 ?
3

in angularjs it is advised to manipulate the DOM elements ,into directives.Into directives we also use jquery. So, you add the 'my-directive' name into the element, like so:

<div my-directive id={{child.id}}container layout="column" flex layout-align="start center" class='container' dragula='"first-bag"' ng-init="vm.getAssociatedWorkItems(child.id); vm.getElementHeight(child.id+'container');">
    <div layout="column" class="md-whiteframe-5dp capitalize itemsInList" ng-repeat="item in vm.items | filter:search" id={{item.id}}child>
        <div class="workItemName">{{vm.getWorkItemName(item.metadata)}}</div>
        <div class="workItemDescription">{{vm.getWorkItemDescription(item.metadata)}}</div>
    </div>
</div>

Into js, you create the directive that gets the height:

.directive('myDirective', function () {
    return {
        restrict: 'AE',
        link: function (scope, element, attrs) {
            var elementHeight = element.height();
            console.log(elementHeight);
        }
    }
})

1 Comment

This would only work if JQuery is available BEFORE angular is loaded. Otherwise, only JQLite is available, which does not have this function.
1

You should use angular's $element service, which is a jqLite wrapper for your component/directive. Try something similar in your controller:

controller: function($element) {
angular.foreach(
    $element.find('.md-whiteframe-5dp, .capitalize, .itemsInList')
        .children(), function(element){
            var el = angular.element(element);
            if(el.hasClass('workItemName')){
                // do something with workItemName DOM element
            }
            if(el.hasClass('workItemDescription')){
                // do something with workItemDescription DOM element
            }
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.