简体   繁体   English

使用

[英]Using <template is=“dom-if” with a condition

I have a <template is="dom-if" if=... that I want to use with a logical condition.我有一个<template is="dom-if" if=...我想与逻辑条件一起使用。

Any binding I try appears to be truthy :我尝试的任何绑定似乎都是真实的

 <link href="https://polygit.org/components/polymer/polymer.html" rel="import"> <dom-module id="test-thing"> <template> <template is="dom-if" if="{{title == 'foo'}}" restamp> Title is <b>FOO</b> </template> <template is="dom-if" if="{{title}} == 'bar'" restamp> Title is <b>BAR</b> </template> Actual: "{{title}}" </template> <script> Polymer({ is: 'test-thing', properties: { title: {type: String,value:''} } }); </script> </dom-module> <div> Title: foo<br> <test-thing title="foo"></test-thing> </div> <div> Title: bar<br> <test-thing title="bar"></test-thing> </div> <div> Title: abc<br> <test-thing title="abc"></test-thing> </div>

What is the correct way to apply an if condition to a dom-if template ?if条件应用于dom-if template的正确方法是什么?

you have to define your function.你必须定义你的功能。

<template is="dom-if" if="[[_isEqualTo(title, 'Foo')]]">

and then in script:然后在脚本中:

function _isEqualTo(title, string) {
  return title == string;
}

whenever is property title changed, function _isEqualTo is called.无论何时更改属性title_isEqualTo调用函数_isEqualTo so you don't have to take care of observing property or something.所以你不必照顾观察财产或其他东西。

function _isEqualTo is called with 2 parameters, the second one is just plain string you want to compare some value with.函数_isEqualTo使用 2 个参数调用,第二个参数只是您想要与某个值进行比较的普通字符串。

The only logic you can use in a binding is not.您可以在绑定中使用的唯一逻辑不是。 for instance:例如:

<template is="dom-if" if="{{_isEqualTo(title, 'Foo')}}">
<template is="dom-if" if="{{!_isEqualTo(title, 'Foo')}}">

If your data was such that Foo was boolean or had an attribute that "Foo":true or "isFoo":true .如果您的数据是 Foo 是布尔值或具有 "Foo":true 或 "isFoo":true 属性。 Then you could just do:那么你可以这样做:

<template is="dom-if" if="{{isFoo}}">
   Foo
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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