0

<dom-if" if="{{_isShowCategory(category.name)}}>即使条件为假,我也无法弄清楚为什么我的内部模板正在呈现。我打印出布尔结果,并且if条件正确评估为falsewhen category.nameis 'account',但仍然是模板呈现。

<dom-if if="[[_shouldRenderDrawer]]">

<template>
    <!-- Two-way bind `drawerOpened` since app-drawer can update `opened` itself. -->
    <app-drawer opened="{{drawerOpened}}" swipe-open tabindex="0">

      <a name="account" href="/account">ACCOUNT</a>

      <iron-selector role="navigation" class="drawer-list" selected="[[categoryName]]" attr-for-selected="name">
        <dom-repeat items="[[categories]]" as="category" initial-count="4">

          <!-- NOTE: I've also tried <dom-if if="{{_isShowCategory(category.name)}}> but I get the same result -->

          <template is="dom-if" if="{{_isShowCategory(category.name)}}">
              <span style="color:black">{{_isShowCategory(category.name)}}</span>
              <a name="[[category.name]]" href="/[[category.name]]">[[category.title]]</a>
          </template>

        </dom-repeat>
      </iron-selector>
    </app-drawer>
  </template>
</dom-if>

  _isShowCategory(categoryName){
    return !Boolean(categoryName === "account");
    // I've also tried return !(categoryName==='account'), which returns the same result as the above
  }
4

1 回答 1

1

只需更改 IF 语句作为回报 (categoryName !== "account"); 这 ”!” symbol 是逻辑 NOT 运算符,这意味着任何为真都将变为假和 videversa,在您的情况下甚至更棘手,因为您有:

如果条件为真

-- (categoryName === "帐户") = TRUE

-- -- Boolean(categoryName === "account") = TRUE

-- -- -- !Boolean(categoryName === "account") = FALSE,因为 NOT 符号 "!"

如果条件是错误的

-- (categoryName === "帐户") = FALSE

-- -- Boolean(categoryName === "account") = TRUE,因为 Boolean("anythingWithAValidValue") 转换任何不同的东西然后 null/undefined 在 TRUE 否则 FALSE

-- -- !Boolean(categoryName === "account") = FALSE,因为 NOT 符号 "!" 正如我之前提到的。

顺便说一句,这不是聚合物问题,请更改问题的标签,JS/JAVASCRIPT 更合适。

于 2017-07-10T18:24:28.897 回答