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

如何在AngularJS中动态使用对象属性作为宽度(在ng repeat中)?,angularjs,ng-style,Angularjs,Ng Style,我无法以ng样式(shape.radius | | shape.length)读取对象的属性。我现在甚至不能让1工作,但我想有一个或声明包括在内。和我的ng课差不多 有一个按钮可以生成形状,形状是以随机大小创建的。这是我的密码: html: 脚本: var app = angular.module('app', []); app.controller('ShapeController', function(){ var vm = this; vm.shapes = []; v

我无法以ng样式(shape.radius | | shape.length)读取对象的属性。我现在甚至不能让1工作,但我想有一个或声明包括在内。和我的ng课差不多

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

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;
}
var-app=angular.module('app',[]);
app.controller('ShapeController',function(){
var vm=这个;
vm.shapes=[];
vm.randomShapes=[];
vm.width=30;
函数createCircle(半径){
设圆=新圆(半径);
vm.shapes.push(圆形);
}//结束创建循环
函数createSquare(长度){
let square=新的正方形(长度);
vm.shapes.push(正方形);
}//结束createSquare
vm.generateShapes=函数(){
让时间=50
for(设i=0;i
宽度
应为
px
(某些单位如
em
pt
等)或
%

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

你确定它没有读吗?或者只是不采用这种风格?检查元素并查看是否应用了样式属性。这修复了它!!谢谢@LindseyOlson请将答案标记为正确,以便帮助他人,谢谢:)
ng-style="{'width': shape.length + 'px'}"