- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试动态设置 left
div 的大小取决于数组中元素的数量。为此,我使用了 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
}
]
}
]
};
最佳答案
之前
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))+'%)'}"
关于html - *ngFor 中的 ngStyle 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42500581/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!