简体   繁体   中英

Polymer 2.0 dom-if rendering template even though if condition is false

I am having trouble figuring out why my inner <dom-if" if="{{_isShowCategory(category.name)}}> template is rendering even though the condition is false. I print out the boolean result and the if condition correctly evaluates to false when category.name is 'account' , but still, the template renders.

<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
  }

just change the IF statement in return (categoryName !== "account"); The "!" symbol is the logical NOT operator, that means that whatever is true will became false and videversa, in your case is even trickier because you have:

IN THE CASE THE CONDITION IS TRUE

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

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

-- -- -- !Boolean(categoryName === "account") = FALSE, because of NOT symbol "!"

IN THE CASE THE CONDITION IS FALSE

-- (categoryName === "account") = FALSE

-- -- Boolean(categoryName === "account") = TRUE, because Boolean("anythingWithAValidValue") converts anything different then null/undefined in TRUE otherwise FALSE

-- -- !Boolean(categoryName === "account") = FALSE, because of NOT symbol "!" as I mentioned before.

Btw this is not a Polymer Issue, please change the tag of your question, JS/JAVASCRIPT is more appropriated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM