我正在尝试根据数组中元素的数量动态设置div的left
。为此,我在calc()
中使用[ngStyle]
方法,其中data.questions
是我绘制的对象数组。
为了证明计算有效,我将其添加到<p>
以进行调试。
我的html中有以下内容:
<div id="progress">
<div *ngFor="let question of data.questions; let i = index;"
[ngStyle]="{'left': 'calc('+(i / data.questions.length * 100) + (1 / (data.questions.length * 2) * 100)+'%)'}">
<p>{{(i / data.questions.length * 100) + (1 / (data.questions.length * 2) * 100)}}%</p>
</div>
</div>
以下是渲染的内容。您可以看到[ngStyle]
仅适用于3个生成的div中的第一个。为什么会这样?
<div _ngcontent-ydx-7="" id="progress" ng-reflect-ng-style="[object Object]" style="border-color: rgb(127, 167, 187);">
<div _ngcontent-ydx-7="" ng-reflect-ng-style="[object Object]" style="left: calc(16.6667%);">
<p _ngcontent-ydx-7="">16.666666666666664%</p>
</div>
<div _ngcontent-ydx-7="" ng-reflect-ng-style="[object Object]">
<p _ngcontent-ydx-7="">49.99999999999999%</p>
</div>
<div _ngcontent-ydx-7="" ng-reflect-ng-style="[object Object]">
<p _ngcontent-ydx-7="">83.33333333333331%</p>
</div>
</div>
注意: 我花了相当多的时间尝试为此制作一个plunkr,但它在我的ngFor中使用data.questions一直给我错误,尽管这肯定有效,如上所述(我的plunkr技能正在进行中......)。
以下是data.questions
的简化版本供人们玩...
private questionOptions: string[] = [
`Strongly Agree`,
`Agree`,
`Slightly Agree`,
`Slightly Disagree`,
`Disagree`,
`Strongly Disagree`
];
private data = {
questions: [{
text: `Great service makes a difference to the customer.`,
options: [{
text: this.questionOptions[0],
score: 3
},
{
text: this.questionOptions[1],
score: 2
},
{
text: this.questionOptions[2],
score: 1
},
{
text: this.questionOptions[3],
score: -1
},
{
text: this.questionOptions[4],
score: -2
},
{
text: this.questionOptions[5],
score: -3
}
]
},
{
text: `Great service can be provided every time we interact with our customers. `,
options: [{
text: this.questionOptions[0],
score: 3
},
{
text: this.questionOptions[1],
score: 2
},
{
text: this.questionOptions[2],
score: 1
},
{
text: this.questionOptions[3],
score: -1
},
{
text: this.questionOptions[4],
score: -2
},
{
text: this.questionOptions[5],
score: -3
}
]
},
{
text: `Customer facing staff have more impact on providing great service than I do.`,
options: [{
text: this.questionOptions[0],
score: -3
},
{
text: this.questionOptions[1],
score: -2
},
{
text: this.questionOptions[2],
score: -1
},
{
text: this.questionOptions[3],
score: 1
},
{
text: this.questionOptions[4],
score: 2
},
{
text: this.questionOptions[5],
score: 3
}
]
}
]
};
&#13;
答案 0 :(得分:3)
在
calc(016.666666666666664%)
calc(33.3333333333333316.666666666666664%)
calc(66.6666666666666616.666666666666664%)
它们不是有效的样式(第一次迭代除外)。您需要将计算括在括号中,如
'calc(' + // string
((i / data.questions.length * 100) + (1 / (data.questions.length * 2) * 100)) + // number
'%)' // string
摘要
[ngStyle]="{'left': 'calc('+((i / data.questions.length * 100) + (1 / (data.questions.length * 2) * 100))+'%)'}"
<强> Plunker Example 强>
答案 1 :(得分:0)
<div id="progress" *ngIf="data">
<div *ngFor="let question of data.questions; let i = index;"
[ngStyle]="{'left': 'calc(i)'}">
<p>{{(i / data.questions.length * 100) + (1 / (data.questions.length * 2) * 100)}}%</p>
</div>
</div>
calcLeftPostion(index: number){
let dataLength = this.data !== null ? this.data.questions.length : 0;
return ((i / dataLength * 100) + (1 / (dataLength * 2) * 100))+ '%';
}