Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I set the height of an element with ng-style?

I am trying to add dynamically set the height of a div, but it is not successfully being set. I get the height of window and divide it by 3, but it is not working. Here is where I set the height:

<div id="wrapper" ng-style="height :hgt+'px'">

I am setting the scope variable like this:

 $scope.hgt = $window.innerHeight / 3;

But the height isn't set.

Plunker http://plnkr.co/edit/eGaPwUdPgknwDnozMJWU?p=preview

like image 274
user944513 Avatar asked Dec 06 '22 21:12

user944513


2 Answers

The usage of ng-style is slightly counter-intuitive. You're attempting to use it to interpolate values, but per the Angular documentation ng-style takes an expression "which evals to an object whose keys are CSS style names and values are corresponding values for those CSS keys."

Thus, you might want to try:

$scope.hgt = { height: ($window.innerHeight / 3) + 'px' };
<div id="wrapper" ng-style="hgt">

Hope this helps!

like image 61
Ryan Miller Avatar answered Dec 25 '22 03:12

Ryan Miller


You should put a dictionary in ng-style like below

<div id="wrapper" ng-style="{height: '{{hgt}}px'}">
  <h4 class="headerTitle">tell Mother name</h4>
</div>
like image 40
Rebornix Avatar answered Dec 25 '22 02:12

Rebornix