首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当if语句正确时,如果语句转到else语句

当if语句正确时,如果语句转到else语句
EN

Stack Overflow用户
提问于 2022-04-19 00:06:49
回答 1查看 39关注 0票数 0

目前,我正试图在手柄中为我的社交媒体应用程序中的消息传递部分编写一个if else块。我正在传递来自控制器的消息,在我所使用的示例中,有一个空数组,但由于某种原因,它会一直传递到else语句。我已经尝试了许多不同的组合来使其工作,但它一直到tried语句。我做错了什么?

代码语言:javascript
复制
{{#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}}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-19 02:39:20

是。是这样的。如果存在空数组,则应该转到else语句,因为空数组的长度为0,即falsy

在你的代码中

代码语言:javascript
复制
{{#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}}

所以它的行为是正确的。

如果您想做相反的事情,要么在ifelse块中交换代码,要么用#unless替换#if

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71918272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档