How to access attributes or methods of polymer element instances
-
21-12-2019 - |
Question
I have defined a new polymer Element.
<polymer-element name="m-test">
<template>
...
</template>
<script type="application/dart" src="m-test.dart"></script>
</polymer-element>
@CustomTag('m-test')
class Mtest extends PolymerElement {
@observable int myAttr = 0;
...
void myMethod()
How can I access the attribute myAttr
or call the method myMethod
for an instance of this element in my main dart program.
<m-test id="mt1"></m-test>
<m-test id="mt2"></m-test>
Solution
You can access your attributes in markup
<m-test id="mt1" my-attr="5"></m-test>
or in code
querySelector('#mt1').myAttr = 5; // produces a warning in the editor
querySelector('#mt1').myMethod(5); // - " -
(querySelector('#mt1') as Mtest).myAttr = 5; // may fail at runtime due to bug https://code.google.com/p/dart/issues/detail?id=15953
(querySelector('#mt1') as Mtest).myMethod(5); // - " -
you can also access event handlers declaratively
void clickHandler(event, detail, target) {
// handle event
}
<polymer-element name="m-test">
<template>
<button on-click="{{clickHandler}}">{{myAttr}}</button> // caption from myAttr
</template>
<script type="application/dart" src="m-test.dart"></script>
</polymer-element>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow