如何将函数对象从 javascript 传递到 Polymer 元素

How to pass a function object to a Polymer element from javascript

我尝试将函数转换为字符串并按如下方式传递:

var myfunc = function (name) {
    alert('It works!' +name);
}

var as_string = myfunc.toString();

var contentHtml = "<my-element post='{\"value\":\"" + target.value.toString() + "\",\"update\":\"" + as_string + "\"}'></my-element>"

但是它没有正确地将它转换成字符串。当我签入开发工具时 显示如下:

<my-element style="z-index:2998; width:700px;" post="{"value":"USUNCLM","update":"function (name) {
                    alert("It works!' +name); }"}'></my-element>

转换为函数在 JS Fiddle 中有效,附于此处: https://jsfiddle.net/kavyapenujuru/L0m95396/

有没有其他方法可以将函数传递给 Polymer 元素? 或者任何其他方法来改进我的解决方案?

这是 Polymer 的工作示例:Plunk

在父元素中定义 JavaScript 对象:

 domReady: function() {
        // Animal properties and method encapsulation
        this.animal = {
          type: "Invertebrates", // Default value of properties
          displayType : function(){  // Method which will display type of Animal
            console.log(this.type);
            alert(this.type);
          }
        }
  },

传递给子元素:

 <my-dynamic_element 
          animal='{{animal}}'>
  </my-dynamic_element>

在子元素内部使用:

 domReady: function() {
      console.log('in Dynamic:');
      this.animal.displayType();
  },

注意,当传递对象时,单引号中的属性会变形:

 obj_attr='{{obj_attr}}'