1

I am trying to get an element's width in my ionic app. But I get an undefined with this code:

html:

$scope.suggestProWidth = angular.element(document.querySelectorAll("suggestPro")[0]).clientWidth;
console.log("$scope.suggestProWidth", $scope.suggestProWidth);

controllers.js:

        <ion-slide ng-repeat="s in [0,1,2,3,4,5,6]">
            <ul>
                <li ng-repeat="item in prodata | matchingProBrand:computed.proName:computed.proBrand | limitTo:s*sliderItemNumber+sliderItemNumber  | limitTo:-sliderItemNumber">
                    <a class="suggestPro" ng-click="launchComputeService(item.id)">
                        <span><img ng-src={{imagesUrls[item.imageName]}} /></span>
                        <p class="flex-caption"> {{item.model}} - {{item.name}}</p>
                    </a>
                </li>
            </ul>   
        </ion-slide>

Can you help me figure this out ?

Thanks

6
  • What is suggestPro? querySelector and querySelectorAll takes .class or #id or tagName or any css selector. Commented Nov 12, 2015 at 18:14
  • suggestPro is a class. Should I put the dot ? Commented Nov 12, 2015 at 18:21
  • This $scope.suggestProWidth = angular.element(document.querySelectorAll(".suggestPro")[0]).clientWidth; console.log("$scope.suggestProWidth", $scope.suggestProWidth); doesn't work either... Commented Nov 12, 2015 at 18:22
  • Possible duplicate of How can I get the width of an element using AngularJS? Commented Nov 12, 2015 at 18:30
  • @isherwood this is exactly what I did but doesn't work Commented Nov 12, 2015 at 18:33

3 Answers 3

2

Initially, create an attribute directive to get width of an element and assign to the <a></a> tag. In the custom directive, you can get width of an element easily.

HTML:

<ion-slide ng-repeat="s in [0,1,2,3,4,5,6]">
  <ul>
    <li ng-repeat="item in prodata |  
      matchingProBrand:computed.proName:computed.proBrand | 
      limitTo:s*sliderItemNumber+sliderItemNumber | limitTo: 
      sliderItemNumber">
        <a custom-item class="suggestPro" 
          ng-click="launchComputeService(item.id)">
          <span><img ng-src={{imagesUrls[item.imageName]}} /></span>
          <p class="flex-caption"> {{item.model}} - {{item.name}}</p>
        </a>
    </li>
  </ul>   
</ion-slide>

JS:

angular.module(modulename)
 .directive('customItem',function(){
   return{
     restrict : 'A',
     link:function(scope,element){
        var eleWidth = element.innerWidth();
        console.log(eleWidth);
     }
   }
 });

Accessing element to get its width in controller is not a good practice. So, best way to get its width is using custom directive.

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

1 Comment

thanks, your solution seems "good practices" so I like it of course. But how can I then use the width to calculate something else in the controller (the number of item displayed in a slider (slide-box from ionic) ?
1

If you want to use it with angular, here is the code;

angular.element(document.getElementById(id)).clientWidth;
angular.element(document.querySelectorAll(".class")[0]).clientWidth;

but I will sugest you put an id for that div, and use the first one. like this:

<li id="suggestion" ng-repeat="item in prodata | matchingProBrand:computed.proName:computed.proBrand | limitTo:s*sliderItemNumber+sliderItemNumber  | limitTo:-sliderItemNumber">
                <a class="suggestPro" ng-click="launchComputeService(item.id)">
                    <span><img ng-src={{imagesUrls[item.imageName]}} /></span>
                    <p class="flex-caption"> {{item.model}} - {{item.name}}</p>
                </a>

and angular element:angular.element(document.getElementById(#suggestion)).clintWidth;

let me if it works with you bro.

Comments

1

Javascript's clientWidth property does not work on HTML elements that have a display type of inline. By default an anchor tag is inline.

You can try adding display:inline-block as a style to the element.

<a ng-click="" style="display:inline-block" />

Be aware clientWidth does not include any border or scrollbar, where as offsetWidth does.

In your example, you have your script before the HTML you are trying to find. This will not work unless you run that particular block after page load. As a simple solution you can move the script block below the HTML.

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.