簡體   English   中英

如何將數據從JavaScript代碼傳遞到Polymer自定義元素?

[英]How to pass data from JavaScript code to Polymer custom element?

我想通過屬性將我在變量上的數據傳遞給Polymer組件。

這是代碼:

<script>
  var item1 = {
      title: "Title 1",
      status: "accepted"
  };
</script>

<bm-card item="{{item1}}" otherAttribute="hello">

otherAttribute獲取自定義元素的數據,但項目未到達。

如何從item1變量填充item屬性?

要在<polymer-element>之外使用數據綁定,您需要<template is="auto-binding">https//www.polymer-project.org/docs/polymer/databinding-advanced.html#bindingoutside

但是,您可以直接在js中設置屬性:

<bm-card otherAttribute="hello">

document.addEventListener('polymer-ready', function() {
  document.querySelector('bm-card').item = item1;
});

您必須等待聚合物就緒的原因是確保元素已在DOM中升級並定義了它的屬性/方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM