简体   繁体   English

Polymer-Dart 1.0 dom-如果不起作用

[英]Polymer-Dart 1.0 dom-if not working

So I was trying to use the <template is="dom-if"> but I just cannot make it work. 所以我试图使用<template is="dom-if">但我无法使其工作。

<template is="dom-repeat" items="{{row}}" as="project">
   <template is="dom-if" if="{{project == null}}">
      <span>Untitled project</span>
   </template>
   <template is="dom-if" if="{{project != null}}">
      <span>{{project.name}}</span>
   </template>
 </template>

For some reasons, I never get shown Untitled project although there are 2 elements which are null . 出于某些原因,我永远不会显示Untitled project尽管有2个元素为null

Are the expressions just not yet available in the developer preview of Polymer-Dart? Polymer-Dart的开发者预览版中是否还没有表达式?

You can actually just use a simple {{project}} and {{!project}} binding. 实际上,您只需使用简单的{{project}}和{{!project}}绑定即可。 ! is one of the only expressions allowed in polymer 1.0. 是聚合物1.0中允许的唯一表达式之一。 It uses JS falsyness so a null value will be "false", but so will an empty list/etc. 它使用JS falsyness,因此null值将为“false”,但空列表/ etc也是如此。

You may also need to explicitly initialize the value to null in your ready method as well: 您可能还需要在ready方法中将值显式初始化为null:

ready() {   if (project == null) set('project', null); }

Basically, for initial values of null we actually send undefined to the js side. 基本上,对于null初始值,我们实际上将undefined发送到js端。 However bindings wont run at all for undefined values, so you need to then explicitly assign null to ensure the dom-ifs will evaluate the expression. 但是,对于未定义的值,绑定根本不会运行,因此您需要显式指定null以确保dom-ifs将计算表达式。 We do this to avoid an extra change notification for each property when each element starts up. 我们这样做是为了避免在每个元素启动时为每个属性提供额外的更改通知。

I don't know about Polymer-Dart, but in Polymer 1.0 expressions are no longer supported ( docs ). 我不知道Polymer-Dart,但是在Polymer 1.0中不再支持表达式( docs )。

You could use computed bindings to achieve the same goal. 您可以使用计算绑定来实现相同的目标。

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

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