如何调用与Dart中特定元素相关联的JS函数?

时间:2016-06-23 10:21:44

标签: dart dart-js-interop

使用最新的软件包:js,如何在Dart中调用与特定html元素/ Web组件关联的JS函数?

示例:在称为OnsenUI的第三方框架中,我可以通过添加

来创建警报对话框
<ons-alertdialog id='blah'>
  Test
</ons-alertdialog>

在html中。此外,我可以通过document.querySelect('blah')访问OnsenUI提供的alertdialog方法.show({options});要么 在javascript中的document.querySelect('blah')。hide({options})。所以我的问题是我需要做什么才能访问Dart中的show / hide方法?

以下是javascript中的代码: https://github.com/OnsenUI/OnsenUI/blob/master/core/src/elements/ons-alert-dialog/index.js

这是他们在angular2中使用打字稿的指令: https://github.com/OnsenUI/OnsenUI/blob/master/bindings/angular2/src/directives/ons-alert-dialog.ts

我的最终目标是模仿他们在angular2中使用打字稿但使用angular2 dart所做的事情。但是,我不确定如何在下面使用Dart实现,因为我不认为直接调用this._onsAlertDialog.show()可以在不使用dart:js context的情况下工作。

/**
   * @method show
   * @signature show()
   * @return Promise<any>
   */
  show() {
    return this._onsAlertDialog.show();
  }

  /**
   * @method hide
   * @signature hide()
   * @return Promise<any>
   */
  hide() {
    return this._onsAlertDialog.hide();
  }

由于

1 个答案:

答案 0 :(得分:0)

使用JS pacakage,您可以创建使用注释镜像JavaScript API的Dart类。例如:

@JS('google.maps')
library maps;

@JS()
class Map {
  external Map(Location location);
  external Location getLocation();
}

现在,如果你写Dart:

var l = getLocation();
var m = new Map(l);

它与JavaScript无关:

var l = google.maps.getLocation();
new google.maps.Map(l);

您需要通过API创建这些类或使用上下文对象。