所以我有这个加载 Instagram 数据的图库,有时 Instagram 照片没有标题,这就是它中断的时候,所以最合乎逻辑的做法是检查标题是否存在,但它仍然中断.
<!-- ko if: $root.entries().length != 0 -->
<!-- ko foreach: $root.entries() -->
<!-- ko with: $root.entries()[$index()] -->
<span data-bind="logger: typeof caption"></span>
<figure>
<img src="" data-bind="attr: {src: images.low_resolution.url}"/>
<figcaption>
<a target="_blank" data-bind="text: user.username, attr: {href: 'http://instagram.com/' + user.username }"></a>
<!--ko if: typeof caption != 'null'-->
<span>
<!-- ko text: caption.text --><!-- /ko -->
</span>
<!--/ko-->
</figcaption>
</figure>
<!-- /ko -->
<!-- /ko -->
<!-- /ko -->
最佳答案
typeof
运算符不返回 null
,因此您的条件实际上是无条件的:它总是尝试呈现 caption.text
甚至如果 caption
为空。
你可以试试:
<!--ko if: caption-->
如果 caption
为“truthy”(不是 false
、null
、,这将扩展评论模板的内容未定义
、0、空字符串等)
关于javascript - knockout 绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210013/