javascript - polymer 铁列表迭代,但不打印

标签 javascript arrays polymer components polymer-1.0

我正在尝试使用 Polymer 中的铁列表将对象数组显示为列表。在 polymer 功能内部,我有...

ready: function() {
    this.list = [{title: 'Thing 1'}, {title: 'Thing 2'}, {title: 'Thing 3'}];
}

在模板中,我有...

<iron-list items="[[ list ]]" as="l">
    <template>
        <div class="row">
            <div class="" on-tap="">
                <span>[[ l.title ]]</span>
                <paper-checkbox class="right"></paper-checkbox>
            </div>
        </div>
    </template>
</iron-list>

我将“iron-list.html”导入到文件中。

我看到的是,iron-list 创建了 3 个模板(没错),但它没有打印出字符串(标题)。这应该很简单,但我在这里迷路了。有人知道为什么它不打印出来吗?

编辑:我遗漏的一件重要事情是,该元素以 display: none 开头,然后切换为稍后显示。

最佳答案

来自here ,它说

iron-list lays out the items when it recives a notification via the resize event. This event is fired by any element that implements IronResizableBehavior.

By default, elements such as iron-pages, paper-tabs or paper-dialog will trigger this event automatically. If you hide the list manually (e.g. you use display: none) you might want to implement IronResizableBehavior or fire this event manually right after the list became visible again. e.g.

所以你需要手动调用

document.querySelector('iron-list').fire('resize');

显示列表后。


此外,即使您没有隐藏/显示列表,您的代码仍然无法工作,因为您将值分配给 list 有点太早了。

原因是在iron-list内部,执行渲染的函数_render只会在this._isVisiblethis._isVisible 基于 iron-list 本身的大小(offsetWidthoffsetHeight)。

因此,请尝试在附加处理程序内分配值,在需要时稍加延迟 -

attached: function () {
    this.async(function () {
        this.list = [{ title: 'Thing 1' }, { title: 'Thing 2' }, { title: 'Thing 3' }];
    }, 100);
}

关于javascript - polymer 铁列表迭代,但不打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32575919/

相关文章:

javascript - 从单独的 JS 文件中检索常量\字符串值

javascript - 如何在socket.io中的断开事件上获取断开连接的客户端的套接字ID

javascript - 使用 JavaScript 更改纸质按钮的文本( polymer 1.0)

Dart 异常 : Already registered (Polymer) prototype for element x

caching - 灯塔和 polymer : start_url in manifest is not cached by Service Worker

javascript - 在 vue.js 应用程序中设置视口(viewport)元标记

javascript - 在 weppy 中,我如何使用 ajax() 从 javascript 将数据发送到 python

arrays - Mongoose save() 不更新数据库文档中数组中的值

Delphi:如何从TBytes中读取X位?

java - 将字符串中的元素用空格分隔成二维数组