2

I cannot get the object's property to be read in ng-style(shape.radius || shape.length). I can't even get 1 to work at the moment, but would like to have an or statement included. Similar to my ng-class.

There is a button to generate shapes, and the shapes were created with a random size. Here is my code:

html:

  <div ng-controller='ShapeController as sc'>
  <div>
    <p><input type="submit" value="Generate Random Shapes" ng-click="sc.generateShapes()"/></p>
    <div ng-repeat="shape in sc.shapes">
      <div class="shape" ng-class="{circle: shape.radius, square: shape.length}" ng-style="{'width': shape.length}"></div>
    </div>
  </div>
</div>

script:

var app = angular.module('app', []);

app.controller('ShapeController', function(){
  var vm = this;
  vm.shapes = [];
  vm.randomShapes = [];
  vm.width = 30;

  function createCircle(radius) {
    let circle = new Circle(radius);
    vm.shapes.push(circle);
  } // end createCircle

  function createSquare(length) {
    let square = new Square(length);
    vm.shapes.push(square);
  } // end createSquare

  vm.generateShapes = function() {
    let times = 50
    for (let i = 0; i < times; i++) {
      createCircle(getRandomNumber());
    }
    for (let i = 0; i < times; i++) {
      createSquare(getRandomNumber());
    }
    sort(vm.shapes);
    console.log(vm.shapes);
  }; // end generateShapes

}); // end controller


function sort(arr) {
  arr.sort(function(a,b){
  return b.getArea() - a.getArea();
  });
} // end sort function

function getRandomNumber() {
  return Math.random() * (100-1) + 1;
}
  • Are you sure it isn't reading it? Or just not applying the style? Inspect the element and see if a style attribute is being applied. – JC Ford Sep 18 '17 at 22:56

1 Answers1

0

width should be either in px(some unit like em, pt, etc) or %

ng-style="{'width': shape.length + 'px'}"
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299