当我在 Chrome 中运行代码(转译为 JS)时,该对象在页面加载时可用。当我在 Chromium(原生 Dart + JS)中运行代码时出现问题,对象未加载,在这种情况下,为 dateObject['length'] 打印 0。
date_picker.dart
part of myproject.common.ui;
@Component(
selector: 'date_picker',
templateUrl: "date_picker.html",
useShadowDom: false
)
class DatePickerComponent{
DatePickerComponent() {
new Future(() {
var dateObject = context.callMethod("jQuery", ['#date_picker']);
print(dateObject['length']);
var dateChanged = dateObject.callMethod("datetimepicker", [new JsObject.jsify({"format": 'MM/DD/YYYY'})]);
dateChanged.callMethod('on', ['dp.change']);
});
}
}
date_picker.html
<div class="form-group">
<div class='input-group date' id='date_picker'>
<input type='text' class="form-control"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
使用评论中的答案添加修改后的 date_picker.dart
part of my_project.common.ui;
@Component(
selector: 'date_picker',
templateUrl: "date_picker.html",
useShadowDom: false
)
@Injectable()
class DatePickerComponent implements ShadowRootAware{
void onShadowRoot(_){
new Future((){
var dateObject = context.callMethod("jQuery", ['#date_picker']);
var dateChanged = dateObject.callMethod("datetimepicker", [new JsObject.jsify({"format": 'MM/DD/YYYY'})]);
dateChanged.callMethod('on', ['dp.change']);
});
}
DatePickerComponent();
}