2

I want to access height and width property of current html img element to apply ngClass dynamically based on property.

I have this code for html

<div class="cfg-pg-outer-box" ng-repeat="album in album_list">
    <div class="cfg-pg-photo-box">
        <img ng-src="{{album.thumbnail.image_path}}" ng-class="myclass(this)">
    </div>
</div>

I am using AngularJS to apply ngClass dynamically

$scope.myclass = function(element) {    
    return (element.width > element.height ? 'landscape' : 'portrait');
}

I am not able to read width and height properties. Do you have any suggestion?

4
  • Does album.thumbnail object have this information maybe like height/width? Do you really need to read it from image object? Commented Feb 19, 2015 at 6:49
  • Try element.offsetWidth/element.offsetHeight Commented Feb 19, 2015 at 6:50
  • @dfsq No album.thumnail do not have this information. Commented Feb 19, 2015 at 6:52
  • @Dr.Molle element.offsetWidth/element.offsetHeight giving undefined Commented Feb 19, 2015 at 6:52

5 Answers 5

2

The problem with your attempt is that unlike regular HTML event attributes like onclick, ngClass and similar have different context of invocation. To be exact this points to current scope object.

In your case you should write a custom directive to read/operate with DOM element. For example simple directive can look like:

app.directive('dimentionClass', function() {
    return {
        link: function(scope, element) {
            var img = element[0];
            img.onload = function() {
                img.className = img.width > img.height ? 'landscape' : 'portrait';
            }
        }
    };
});

and you will use it like this:

<img ng-src="{{album.thumbnail.image_path}}" dimention-class />

Demo: http://plnkr.co/edit/GtklqTUvtbFxb6AFuz4S?p=preview

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

Comments

1

use element.offsetWidth and element.offsetHeight

2 Comments

Giving undefined inside $scope.myclass function.
try element[0].offsetWidth
1

You need to use $scope.$apply, otherwise any changes to $scope made in non-Angular event handlers won't be processed properly:

$scope.myclass = function(element) {

$scope.image.width = img.width;
$scope.image.height = img.height;
$scope.image.path = $scope.imageurl;
$scope.$apply();

   return (this.width > this.height ? 'landscape' : 'portrait');

}

Comments

1
var width = document.getElementById('<Element's ID>').offsetWidth;
var height= document.getElementById('<Element's ID>').offseHeight;

Comments

0
var height = document.body.clientHeight;
var width = document.body.clientWidth;

Explanation

EDIT:

In response to your comment, try using:

var element = document.getElementById("elementId");

var height = element.style.height;
var width =  element.style.width;

EDIT2:

You might also wanna try:

document.getElementById(id_attribute_value).offsetHeight;

and/or

document.getElementById(id_attribute_value).clientHeight;

1 Comment

I believe it gives height/width of whole window , but I needs height/width of that element only.

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.