0

在 0.5 中,我可以使用 dom-if 中的表达式在循环遍历数组时选择数组中的某些内容。我怎样才能在 1.0 中达到同样的效果?

4

2 回答 2

2

使用该filter/observe功能dom-repeat而不是嵌套更有效dom-iffilter指定一种方法,用于识别要从您的集合中显示的记录,observe告诉dom-repeat观察哪些数据以了解何时重新运行过滤器。例如

<template is="dom-repeat" items="{{records}}" filter="hasPersonLabel" observe="item.labels">
...
hasPersonLabel: function (labels) {
    return (labels.indexOf("Person") >= 0);
}

文档在这里(https://www.polymer-project.org/1.0/docs/devguide/templates.html#filtering-and-sorting-lists)。

于 2015-06-14T10:08:19.343 回答
1

您使用类似的功能

hasPersonLabel: function (labels) {
    if (labels.indexOf("Person") === -1) {
        return false
    }
    return true
}

然后你可以使用

<template is="dom-repeat" items="{{records}}">
    <template is="dom-if" if="{{isPerson(item.labels)}}">
于 2015-06-14T09:23:33.497 回答