【发布时间】:2021-05-08 23:15:05
【问题描述】:
我正在使用名为 this.expanded 的 Knockout observable 来处理是否呈现某些 HTML,并使用 aria-expanded 属性中的值。
<button data-bind="attr: { 'aria-expanded': expanded }">
<!-- ko if : expanded -->
<div>...</div>
<!-- /ko -->
在这两种情况下使用我的变量expanded 的问题是我注意到当expanded 为假时,我根本没有得到aria-expanded 属性的输出。
// When expanded is true:
<button aria-expanded="true"></button>
// When expanded is false:
<button></button>
// What I want:
<button aria-expanded="false"></button>
当expanded 为假时,我怎样才能仅使用false 输出属性?
我尝试创建一个辅助变量来检查 observable 并将其转换为字符串:
this.expandedStatus = this.expanded().toString()
但我注意到expandedStatus 如果以这种方式使用,似乎不再更新/观察。
【问题讨论】:
标签: knockout.js