0

http://plnkr.co/edit/LZUa1tm7EROk8zcw6sGh?p=preview

angular.module('myModule', [])
  .controller('myDirective', function() {
    console.log(angular.element('.myClass').prop('offsetWidth'));
  }); 

I want to check where the DOM is visible or not in angularjs. But got an error of myClass is not a function?

2
  • Can you show an console output of the problem? Commented Nov 15, 2016 at 7:47
  • 1
    Your controller name in myDirective and you have set ng-controller as myClass Commented Nov 15, 2016 at 7:47

2 Answers 2

1

Change your controller name myDirective to myClass.

Try like this

angular.module('myModule', [])
  .controller('myClass', function($scope) {
    var mClass = document.getElementsByClassName("myClass");
    console.log(angular.element(mClass).prop('offsetWidth'));

  });

DEMO

N:B: DOM related task should be handled in the directive instead of controller.

Sign up to request clarification or add additional context in comments.

Comments

0

Angular.element is different from document.getElementsByClassName. angular.element takes in an element, for eg-

var elem = angular.element('<div class="myDiv">{{ model.input }}</div>')

The above one is correct, but lets see the wrong version too

var elem = angular.element('.myDiv')

This one wont work

For your case do it like this :-

angular.element($document.find('div.myClass'))

Note :- you will have to inject $document in the controller, this way you can keep your whole code in angular format instead of using proper javascript methods like document.getElementsByClassName()

Hope it helps

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.