3

我在模板中继器中有几个有条件标记的元素。现在,当我更新数据时,if 条件似乎没有生效,这导致undefined传递到处理这些元素数据的函数中。

使用该restamp属性似乎没有帮助(https://www.polymer-project.org/1.0/docs/devguide/templates.html#dom-if)。到目前为止,我只能通过清空this.items = [];发起新请求的更改处理程序中的 items 属性 () 来解决这个问题。

This works, but results in the template being empty for a short amount of time before the new data gets displayed. Not necessarily a problem, but I wonder if I'm doing something wrong.

Here are the corresponding parts of the code:

<template>
...
<iron-ajax
  id="ironajax"
  url="https://www.url.com/api"
  params="{{ajaxParams}}"
  handle-as="json"
  on-response="handleResponse"
  on-error="handleError">
</iron-ajax>
...
<template is="dom-repeat" items="{{items}}" id="items">
...
<template is="dom-if" if="{{item.info.subtitle}}">: 
  <span>{{truncateSubtitle(item.info.subtitle)}}</span
</template>
...
Polymer({
  is: 'my-element',
  properties: {
    query: {
      type: String,
      value: ''
    }
    ...
    items: {
      type: Array,
      value: []
    }
  }
  ...
  handleChange: function() {
    if (this.value !== '') {
      this.items = [];
      this.query = this.value;
      this.$.ironajax.generateRequest();
    }
  }
...
4

2 回答 2

5

给定"{{item.info.subtitle}}", if item.infoisnullundefinedif item.info.subtitleis undefined,绑定将不会刷新并且可能具有陈旧值(在重复使用节点的重复上下文中)。

Polymer 不会对undefined值执行计算,因为它在大量情况下是一种性能增强,但是在这种情况下它可能会很棘手。

您应该使用函数来解析正确的状态。就像是

if="{{hasSubtitle(item)}}"

hasSubtitle function(item) {
  return item.info && item.info.subtitle;
}
于 2015-06-29T06:58:39.757 回答
0

“dom-if”的“if”属性需要一个布尔值。

如果“item.info.subtitle”不为空或为空,请尝试创建将设置为 true 或 false 的聚合物属性。

然后将 created 属性与 dom-if 一起使用:

<template is="dom-if" if="{{hasSubtitle}}" >

参考: http: //polymer.github.io/polymer/ --> dom-if(下拉)


附加信息:6月10日添加

尝试使用属性而不是方法。

像这样声明你的财产:

Polymer({
    is: "my-element",
    properties: {
        hasSubtitle: {
            type: Boolean,
            value: 'false',  //false or true, the more common case.
        }
    },
    ...
});
于 2015-06-09T13:22:47.753 回答