目前,我正试图在手柄中为我的社交媒体应用程序中的消息传递部分编写一个if else块。我正在传递来自控制器的消息,在我所使用的示例中,有一个空数组,但由于某种原因,它会一直传递到else语句。我已经尝试了许多不同的组合来使其工作,但它一直到tried语句。我做错了什么?
{{#if messages.length}}
<p>if statement</p>
<div class="message-container card" id="{{this._id}}">
<p> Start a new conversation </p>
</div>
{{else}}
<p>else statement</p>
{{#each messages}}
<div class="message-container card" id="{{this._id}}">
<div class="profile-picture">
<img src={{user.profilePicture}} width="100"
height="100"
class="rounded-circle"/>
</div>
<div class="message-header">
<span class="name">{{this.sender.firstName}} {{this.sender.lastName}}</span>
<div class="date-sent">{{this.createdAt}}</div>
</div>
<div class="post-body">
<p class="message">
{{this.message}}
</p>
</div>
</div>
{{/each}}
{{/if}}发布于 2022-04-19 02:39:20
是。是这样的。如果存在空数组,则应该转到else语句,因为空数组的长度为0,即falsy。
在你的代码中
{{#if messages.length}}
print this if array is not empty because
message.length is not zero
{{#else}}
print this if array is empty
because message.length is zero
{{/if}}所以它的行为是正确的。
如果您想做相反的事情,要么在if和else块中交换代码,要么用#unless替换#if。
https://stackoverflow.com/questions/71918272
复制相似问题