简体   繁体   English

访问聚合物内部的全局变量

[英]Access global variable inside polymer

I need to access javascript variable inside Polymer object. 我需要访问Polymer对象内的javascript变量。 Tried accessing that variable inside ready function but nothing comes. 尝试在ready函数中访问该变量,但没有任何反应。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="components/webcomponentsjs/webcomponents-lite.js"></script>
    <link rel="import" href="elements/my-app.html">
  </head>
  <body>
    <my-app></my-app>
    <script>
      var messages = []
      var messages_count = {{ messages|length }};

      {% for message in messages %}
          var message = "{{ message.text }}"
          messages.push(message)
      {% endfor %}
    </script>
  </body>
</html>

Update: Added my-app html file 更新:添加了my-app html文件

<dom-module is="my-app">
   <script>
     Polymer({
       is: "my-app",
     })
   </script>
</dom-module>

And How do I access the messages in my-app ? 以及如何访问my-appmessages

You can access them just like you access them in any other javascript 您可以像访问其他任何JavaScript一样访问它们

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base href="https://polygit.org/components/"> <script src="wecomponentsjs/webcomponents-lite.min.js"></script> <link rel="import" href="polymer/polymer.html"> </head> <body> <my-app></my-app> <script> var message = "Hi"; </script> </body> </html> <dom-module is="my-app"> <template></template> <script> Polymer({ is: "my-app", attached: function() { alert(message); } }); </script> </dom-module> 

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

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