19

在 Dart 的 Web UI 中,可以将任意数据传递给函数以响应事件,例如,以下代码段将值传递2increment(int incBy)方法以响应按钮的点击事件:

<!-- Web UI -->
<element name="x-click-counter">
  <template>
    <button on-click="increment(2)">  <!-- passing a value of 2 -->
      Click me
    </button>   
  </template>
</element>
<script>
  import 'package:web_ui/web_ui.dart';

  class CounterComponent extends WebComponent {
    void increment(int incBy) {        // accept the value of 2
      count = count + incBy;
    }
  }
</script>

在 Polymer(和 Polymer.dart)中,点击事件属性需要函数名的字符串版本,而不是实际的函数调用。这在聚合物文档页面上描述为:

事件处理程序属性的值是组件上方法的字符串名称。与传统语法不同,您不能将可执行代码放入属性中。

使用polymer.dart,这看起来像:

<polymer-element name="x-click-counter">
  <template>
    <button on-click="increment">  <!-- can't pass a value of 2, as you need to pass a string -->
      Click Me
    </button>
  </template>
</polymer-element>
<script>
  import 'package:polymer/polymer.dart';

  @CustomTag("x-click-counter")
  class CounterComponent extends PolymerElement with ObservableMixin {
    @observable int count = 0;

    void increment(Event event, var detail, var target) {  // How do I pass 2 to this function?
      count = count ++;
    }
  }
</script>

问题:如何将任意值传递给increment函数?

4

3 回答 3

19

您可以使用htmldata-属性传递额外的数据,然后通过target参数访问它们。

重写聚合物示例以添加一个data-incby字段,该字段将值递增计数,如下所示:

<polymer-element name="x-click-counter">
  <template>
    <button on-click="increment"  data-incby="2">  <!-- now passing the value as a data attribute -->
      Click Me
    </button>
  </template>
</polymer-element>
<script>
  import 'package:polymer/polymer.dart';

  @CustomTag("x-click-counter")
  class CounterComponent extends PolymerElement with ObservableMixin {
    @observable int count = 0;

    void increment(Event event, var detail, var target) {
      int incBy = int.parse(target.attributes['data-incby']);  // extract the value 2
      count = count + incBy;
    }
  }
</script>
于 2013-08-15T08:39:33.807 回答
14

Dart 和 Polymer.dart 自从 Chris 回答后发生了变化。这是 Dart v1.0 的更新代码:

<polymer-element name="x-click-counter">
  <template>
    <button on-click="{{increment}}"  data-incby="2">  <!-- now passing the value as a data attribute -->
      Click Me
    </button>
    <span>{{count}}</span>
  </template>
</polymer-element>
<script type="application/dart">
  import 'package:polymer/polymer.dart';
  import 'dart:html';

  @CustomTag("x-click-counter")
  class CounterComponent extends PolymerElement {
    @observable int count = 0;

    CounterComponent.created() : super.created();

    void increment(Event event, var detail, var target) {
      int incBy = int.parse(target.attributes['data-incby']);  // extract the value 2
      count = count + incBy;
    }
  }
</script>
于 2013-12-13T22:15:37.783 回答
1

我的聚合物 0.11.0+5 解决方案

元素.html

<link rel="import" href="../packages/polymer/polymer.html">
<polymer-element name="dp-element">
  <template>
    <div class="row">
    <ul>
      <template repeat="{{ item in items }}">
        <li on-click="{{load}}" data-incby="{{item}}">{{ item }}</li>
      </template>
    </ul>

  </template>

 <script type="application/dart">
  import 'package:polymer/polymer.dart';
  import 'view.dart';
  import 'dart:html';

  @CustomTag('dp-element')
  class DpElement extends PolymerElement {

  @observable List<String> items;

  DpElement.created() : super.created(){
  }
  void load(Event event, var detail, var target) {
     String incBy = target.attributes['data-incby'];
     print(incBy);
  }
}
于 2014-07-11T14:03:40.807 回答