Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access map values / iterate over map in Polymer.dart 0.5

Tags:

polymer

dart

I am working on my first polymer application and I am stuck. How do I access map values with polymer? I have a class with a map within. I can iterate over lists in my template, but how to get all values from a map?

 query('#tmpl-question').model = q;
 query('#tmpl-answers').model = q.map_answers;


@observable
class Question extends PolymerElement with ObservableMixin{

  @observable
  Map<String,String> map_answers = toObservable({}); //new Map<String,String>();

  void initQuestion(){

    map_answers["false1"]="1";
    map_answers["true"]="42";
    map_answers["false2"]="2";}}

        <template id="tmpl-answers" repeat>
        <p>
           <label for"a1" value="asdf"> HOW TO DISPLAY ALL MAP VALUES  //iterate over list elements with {{}}
        </p>
       </template>

EDIT: the problem was with the assignment of the object q to the model.

does not work:

Question q = new Question();
   query('#tmpl-question').model = q;

if you import 'package:polymer_expressions/polymer_expressions.dart'; this will work:

  TemplateElement template = query('#tmpl-question');
  template.bindingDelegate = new PolymerExpressions(globals: {
    'uppercase': (String input) => input.toUpperCase()
  });
  template.model = q;

.

 <template repeat="{{j in map2.keys}}">
        <li>{{map2[j]}}</li>
      </template>
like image 366
Gero Avatar asked Dec 11 '25 21:12

Gero


1 Answers

Maps have two iterable properties: keys and values. I think what you want to do is iterate over the keys and then access the values.

Something like:

<polymer-element ... >
  <template>
    <template repeat="{{ key in map_answers.keys }}">
      <p>
        <label>{{ map_answers[key] }}</label>
      </p>
    </template>
  </template>
</polymer-element>
like image 180
Justin Fagnani Avatar answered Dec 14 '25 05:12

Justin Fagnani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!