JavaScript Getting element width inside directive the Angular way get element width javascript,get element width,get element width

I have a directive which contains an element inside it with width: 100%. However, the directive must know the clientWidth of that element -- that is, in pixels rather than percent. The element is buried deep in the directive's DOM.

The containing controller may have multiple instances of this directive, so doing document.getElementsByClassName('myclass')[0].clientWidth won't work. Selecting the directive's element works, but the code for that looks similar to element.children().children().children()[32].clientWidth, which doesn't look very stable. What is the right, Angular way of doing this?

Answer:1

Whenever you need to do any DOM manipulation in Angular, directives are the way to go. The "Angular" approach would be to use your directive's link function.

DEMO: http://plnkr.co/edit/83aZUmsRzuTUFotxUYSB?p=preview

Template Use Case:

<div element-width></div>

Directive Example:

angular.module('myApp').directive('elementWidth',function(){
    return {
        restrict: "A",
        link:function($scope, element){
          // element param being passed in refers 
          // to the element the directive is bound to, in this case, the DIV


          // do something with the element width here
        }
    }
});

Hope this helps and let me know if you have any questions.

Answer:2

I need to know if an element is styled with a :first-letter style, and it should be a general solution so I won't depend on class names or special style attributes. Is there any way? Example: <p ...

I need to know if an element is styled with a :first-letter style, and it should be a general solution so I won't depend on class names or special style attributes. Is there any way? Example: <p ...

Here is my question... Given an array populated with numbers as a function parameter, produce a resulting array which contains any duplicates number from the array. For example, given the array [ 1,...

Here is my question... Given an array populated with numbers as a function parameter, produce a resulting array which contains any duplicates number from the array. For example, given the array [ 1,...

  1. javascript arrays find
  2. javascript arrays find all
  3. javascript array find index
  4. javascript array find value
  5. javascript array find element
  6. javascript array find first
  7. javascript array find object
  8. javascript array find and remove
  9. javascript array find duplicates
  10. javascript array find index of object
  11. javascript array find example
  12. javascript array find by property
  13. javascript array find method
  14. javascript array find one
  15. javascript array find and replace
  16. javascript array find index of object by property
  17. javascript array find string
  18. javascript array find object with property value
  19. javascript array find max
  20. javascript array find value by key

I have the HTML table & its CSS. CSS : #outerDIV { position: relative; padding-top: 20px; } #innerDIV { overflow-y: auto; height: 500px; } table#mytable tbody tr:first-child { width:...

I have the HTML table & its CSS. CSS : #outerDIV { position: relative; padding-top: 20px; } #innerDIV { overflow-y: auto; height: 500px; } table#mytable tbody tr:first-child { width:...

  1. html width percent
  2. html width and height
  3. html width auto
  4. html width 100
  5. html width units
  6. html width property
  7. html width fit content
  8. html width 100 percent
  9. html width of div
  10. html width not working
  11. html width 100 not working
  12. html width code
  13. html width auto fit
  14. html width em
  15. html width style
  16. html width of text input
  17. html width full
  18. html width image
  19. html width table
  20. html width wrap content

My script doesn't work in Wordpress. I'm trying to convert my HTML page into a Wordpress Theme.. Ive got the CSS linked.. <link type="text/css" rel="stylesheet" href="<?php bloginfo('...

My script doesn't work in Wordpress. I'm trying to convert my HTML page into a Wordpress Theme.. Ive got the CSS linked.. <link type="text/css" rel="stylesheet" href="<?php bloginfo('...

  1. link script in wordpress