如何在AngularJS中动态使用对象属性作为宽度(在ng-repeat中)?

时间:2017-09-18 22:40:47

标签: angularjs ng-style

我无法以ng-style(shape.radius || shape.length)读取对象的属性。我现在甚至无法上班,但是想要包含或声明。与我的ng-class相似。

有一个生成形状的按钮,形状是随机大小创建的。这是我的代码:

HTML:

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;
}

脚本:

{{1}}

1 个答案:

答案 0 :(得分:0)

width应该在px(某些单位,如empt等)或%

ng-style="{'width': shape.length + 'px'}"