【发布时间】:2016-06-13 03:52:52
【问题描述】:
我正在尝试在 Angular2 中制作圆板。例如,我想制作 10 个圆圈,但实际上这个数字可以改变。我想计算每个圆的半径,所以它是动态的而不是静态的。以图片为例
这是我的代码
@Component({
selector:"circle"
template: `
<svg>
<circle *ngFor='#item of forLength #i=index #l=last #e=even'
cx="50%" cy="50%" [style.r]="calculateRadius()" stroke="black" stroke-width="5" fill="white"></circle>
<svg/>
`
})
export class CircleComponent{
public maxRadius:number=25;
public totalRounds:number=10;
public x:number=30;
public calculateRadius():number{
var distanceBetweenCircles=this.maxRadius/(this.totalRounds-1);
this.x-= distanceBetweenCircles;
return this.x;
}
}
但我收到以下错误:
calculateRadius() in CircleComponent@7:30' has changed after it was checked.
Previous value: '-7.500000000000007'.
Current value: '-36.66666666666668' in [calculateRadius() in CircleComponent@7:30]
有没有更好的方法用*ngFor 编写这个for 循环,而不是用单独的方法编写这个?
【问题讨论】:
-
您在模板中的哪里调用
calculateRadius()?您在模板中的何处设置 SVGr属性? -
@MarkRajcok 对不起,我在代码中写了一个错误,我现在已经更新了代码。我在
标签 中使用 calculateRadius()
标签: angularjs data-binding typescript angular