[英]Polymer dom-if: how do I implement a negative condition?
我有一個Polymer元素,使用<template is="dom-if"...
根據條件提供不同的HTML內容。
聚合物dom-if
沒有其他條件,因此需要一個負的if條件來模擬它。
像這樣的東西:
<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>
只有這不起作用 - 負面條件永遠不會通過。
該如何實施?
您必須為title屬性使用默認空值:
title:{type: String,value:''}
像這樣:
<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.