【问题标题】:Accessing data bound attributes without using the 'this.attributeName' notation in Polymer在不使用 Polymer 中的“this.attributeName”表示法的情况下访问数据绑定属性
【发布时间】:2018-02-16 04:09:56
【问题描述】:

我正在尝试将 sweetalert2 与自定义 html 一起使用,该 html 包含我想要访问的具有数据绑定的聚合物元素,但我无法使用“this.attributeName”表示法获取数据。我认为这是由于在创建警报的元素上方的 DOM 上附加了甜蜜警报,但我可能错了,是否有解决方法?我已经提供了用于创建警报的代码,并且在解决方案中我试图解决这个问题。

            swal({
                title: 'Blah Blah',
                html: 
                '<some-el id="days" chosen-days="{{newCDays}}"></some-el>\
                <time-input id="start" time$="{{newSTime}}" hour12 clamp="seconds"></time-input>\
                <time-input id="end" time$="{{newETime}}" hour12 clamp="seconds"></time-input>',
                confirmButtonText: 'Done',
                preConfirm: function() {
                    return new Promise((resolve, reject) => {
                        resolve({
                            days: document.getElementById('days').getAttribute("chosen-days"),
                            start: document.getElementById('start').value,
                            end: document.getElementById('end').time,
                        });
                    });
                }
            });

【问题讨论】:

  • 您应该可以使用document.getElementById('days').chosenDays 访问该属性。见demo。使用swal之前是否验证过元素定义已经导入?
  • 所以 'document.getElementById('days').chosenDays' 给我 undefined 和 'document.getElementById('days').newCDays' 给我一个错误说“property-accessors.html:259 Polymer::Attributes: 无法将 Array 解码为 JSON: {{newCDays}}"。我假设元素定义在那里,因为我可以看到 html 元素,这都包含在作为视图的聚合物元素中,不确定这是否有帮助。
  • 你能在codepen中创建一个repro吗?
  • codepen.io/loganxdev/pen/gvXXGN 这有帮助吗?您可以看到我在 html 属性下的 sweet alert 函数中放置的绑定只是作为字符串显示在输入中,而不是创建绑定。

标签: javascript data-binding polymer


【解决方案1】:

swal html 中没有数据绑定

Polymer 数据绑定只能在 Polymer 模板内使用,因此在 swalhtml 字段中使用该语法会导致将文字值分配给属性。在以下示例中,&lt;paper-input&gt;.value 按字面意思设置为 "{{myValue}}"(包括大括号)。请注意,它不会创建 &lt;paper-input&gt;.myValue

swal({
  html: '<paper-input no-label-float value="{{myValue}}"></paper-input>'
  ...
})

html 不应包含数据绑定。它应该是这样的:

swal({
  html: '<paper-input no-label-float></paper-input>'
  ...
})

访问元素属性

preConfirm 函数中,您可以从html 字段中查询元素,并通过名称访问该元素的属性:

swal({
  preConfirm: async () => {
    console.log({
      noLabelFloat: document.getElementById('myInput').noLabelFloat
    });
  }
  ...
})

本机事件绑定

在您的Codepen 中,您表明&lt;input&gt; 是这样使用的:

<input type="text" value$="{{testAttr}}">

根据您在swalhtml 中的绑定,我认为您假设上面的数据绑定会将&lt;x-foo&gt;.testAttr 设置为&lt;input&gt;.value,但这实际上是不正确的。给定一个原生 &lt;input&gt; 元素,你必须使用 Polymer 的 native event-binding syntax

<input type="text" value="{{testAttr::input}}">

&lt;input&gt; 引发input 事件时,将&lt;x-foo&gt;.testAttr 设置为&lt;input&gt;.value。相当于:

// in x-foo script
myNativeInput.addEventListener('input', () => {
  this.testAttr = myNativeInput.value;
});

demo

【讨论】:

  • 在如何与该 codepen 中的非聚合物元素结合方面留有余地,但现在一切正常,谢谢。
猜你喜欢
  • 2016-07-03
  • 2013-04-01
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多