简体   繁体   English

Polymer - DOM-IF中的项在第一次调用时返回null

[英]Polymer - items in DOM-IF returning null in first call

I'm writing because I have problem with understanding how Polymer ready and elements exactly works. 我正在写作,因为我对理解Polymer ready和元素的确切工作方式存在问题。 I'm using Polymer 2.0. 我正在使用Polymer 2.0。

In my template I have this code: 在我的模板中,我有这个代码:

<template is="dom-if" if="{{isExpanded}}">
        <div id="drop-down-list" >
            <template is="dom-repeat" items="{{employees}}">
                <button on-click="onDropDownItemClick" class="button-style items">{{item}}</button>
            </template>
        </div>
    </template>

On start the isExpandend is false . 启动时, isExpandendfalse So the buttons are hidden, or even not created? 按钮是隐藏的,甚至没有创建?

Then in some function I switch isExpandend to true and I want to update some styles in that way: 然后在一些函数中我将isExpandend切换为true ,我想以这种方式更新一些样式:

var items = this.shadowRoot.querySelectorAll('.items');
            for(var i=0; i< items.length; i++){
                items[i].style.height = itemHeight;
            }

The problem is - this solution only works with second or more entry to this function. 问题是 - 此解决方案仅适用于此功能的第二次或更多次输入。 So when I call this function first time, items are null, and nothing changed, but when I call second time and more items = [], and code works good. 因此,当我第一次调用此函数时,items为null,并且没有任何更改,但是当我第二次调用更多items = []时,代码运行良好。

What should I do to repair this bug? 我该怎么做才能修复这个bug?

Should I do something with "async" I find this on stack but i have no idea how to use it. 我应该用“异步”做什么我在堆栈上找到它但我不知道如何使用它。

I found one possible solution for this problem. 我找到了一个可能解决这个问题的方法。 I added id too template with dom-if 我用dom-if添加了id too template

and before this JS code I write: 在我写这个JS代码之前:

this.$["templateID"].render();

This is working, but I don't know it is good? 这是有效的,但我不知道它是好的吗?

How do you perform the switch of isExpanded ? 你如何执行isExpanded的切换? Usually when something doesn't work at first but then updates later on interaction that means that you mutated the value incorrectly and maybe you should be using set or push api's to notify about changes. 通常当某些东西最初不起作用但稍后在交互时更新意味着你错误地改变了值,也许你应该使用setpush api来通知有关更改。

Do you think you can create a plunker with the example? 你认为你可以用这个例子创建一个plunker吗?

Correct behavior for Polymer, the items are not added to the DOM so do not have a height. 对于Polymer的正确行为,项目不会添加到DOM中,因此没有高度。

Possibly you can add a dom-change event 可能你可以添加一个dom-change事件

But more ideal is that you just style the height of the individual elements using CSS. 但更理想的是,您只需使用CSS设计单个元素的高度。 Why set it in a for loop? 为什么要在for循环中设置它?

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

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