将<模板与条件一起使用 [英] Using <template is="dom-if" with a condition

查看:0
本文介绍了将<模板与条件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个<template is="dom-if" if=...要与逻辑条件一起使用。

我尝试的任何绑定似乎都是真实的

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
<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>

if条件应用于dom-iftemplate的正确方法是什么?

推荐答案

您必须定义您的函数。

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

然后在脚本中:

function _isEqualTo(title, string) {
  return title == string;
}
只要属性title发生更改,就会调用函数_isEqualTo。因此,您不必注意观察财产或其他东西。

函数_isEqualTo使用两个参数调用,第二个参数只是要与某些值进行比较的纯字符串。

这篇关于将&lt;模板与条件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆