将DartAngular与dart:html一起使用 [英] Use DartAngular with dart:html

查看:119
本文介绍了将DartAngular与dart:html一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将默认的飞镖库html与角度飞镖一起使用? 即:

Is it possible to use default dart library html with angular dart? ie:

class Test1Component implements OnInit{
  @override
  void ngOnInit() {
    ButtonElement button = querySelector('button');
    //Broken code, avoid button to be null.
    button.onClick.listen(onClick);
  }

  void onClick(Event e){
    print('Button clicked');
  }
}

如何避免不使用任何计时器而获得空"按钮?

How can I avoid to get a 'null' button without the using any timers?

基本上,我只为Routes使用角度,但我想坚持使用dart:html来控制DOM和事件.

Basically I'm using only angular just for the Routes and but I'd like to stick with dart:html to control the DOM and events.

推荐答案

是的,您可以这样做,但这通常不是一个好主意.

Yes, you can do that, but it's usually not a good idea.

改用@ViewChild(...)或类似的Angular方法获取对组件视图中元素的引用.

Use instead @ViewChild(...) or similar Angular methods to get references to elements in a components view.

<button #myButton>click me</button>

@ViewChildren('myButton')
set myButton(List<Element> value) {
  if(value.isNotEmpty) {
    print(value.first);
  }
}

如果您只想使用添加点击处理程序

If you want to just add a click handler using

<button (click)="onClick">click me</button>

将是更好的方法,但听起来您正在以某种方式动态添加按钮,并且声明性地添加单击处理程序在这种情况下可能不起作用(将需要更多信息)

would be the better way but it sounds you are somehow adding the button dynamically and adding a click handler declaratively might not work in this case (would need more info)

这篇关于将DartAngular与dart:html一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆