[英]Polymer dom-if: how do I implement a negative condition?
I have a Polymer element that uses <template is="dom-if"...
to provide different HTML content depending on a condition. 我有一个Polymer元素,使用<template is="dom-if"...
根据条件提供不同的HTML内容。
Polymer dom-if
has no else condition, so needs a negative if condition to simulate it. 聚合物dom-if
没有其他条件,因此需要一个负的if条件来模拟它。
Something like this: 像这样的东西:
<link href="https://polygit.org/components/polymer/polymer.html" rel="import"> <dom-module id="test-thing"> <template> <template is="dom-if" if="{{title}}" restamp> <b>[[title]]</b> </template> <template is="dom-if" if="{{!title}}" restamp> <i>no title</i> </template> </template> <script> Polymer({ is: 'test-thing', properties: { title: String } }); </script> </dom-module> <div> With negative condition: <test-thing></test-thing> </div> <div> With positive condition: <test-thing title="Has Title"></test-thing> </div>
Only that doesn't work - the negative condition never passes. 只有这不起作用 - 负面条件永远不会通过。
How should this be implemented? 该如何实施?
You must use a default empty value for your title property: 您必须为title属性使用默认空值:
title:{type: String,value:''}
Like so: 像这样:
<link href="https://polygit.org/components/polymer/polymer.html" rel="import"> <dom-module id="test-thing"> <template> <template is="dom-if" if="{{title}}" restamp> <b>[[title]]</b> </template> <template is="dom-if" if="{{!title}}" restamp> <i>no title</i> </template> </template> <script> Polymer({ is: 'test-thing', properties: { title: {type: String,value:''} } }); </script> </dom-module> <div> With negative condition: <test-thing></test-thing> </div> <div> With positive condition: <test-thing title="Has Title"></test-thing> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.