NgComponent准备好的事件(NgComponent ready event)

我正在尝试创建一个NgComponent ,它在加载组件时填充元素。 如何知道组件何时填充模板? 这是我的一些代码:

class SleepTimeModule extends Module {
  SleepTimeModule() {
    //...
    type(SleepGraph);
    //...
  }
}

//...

@NgComponent(
  selector: 'sleep-graph',
  publishAs: 'sleepGraph',
  template: '<div><svg></svg></div>'
)
class SleepGraph {
  Element element;
  Scope scope;

  @NgTwoWay('data')
  var data;

  SleepGraph(this.element, this.scope);

  // when can I call this method?
  // it modifies the dom
  drawChart(data){
    js.context.callMethod("drawChart", [
      element.shadowRoot.querySelector("svg"),
      new js.JsObject.jsify(data)
    ]);
  }
}

I am trying to create a NgComponent which is populated with elements when the component is loaded. How do I know when the component is populated with the template? Here is some of my code:

class SleepTimeModule extends Module {
  SleepTimeModule() {
    //...
    type(SleepGraph);
    //...
  }
}

//...

@NgComponent(
  selector: 'sleep-graph',
  publishAs: 'sleepGraph',
  template: '<div><svg></svg></div>'
)
class SleepGraph {
  Element element;
  Scope scope;

  @NgTwoWay('data')
  var data;

  SleepGraph(this.element, this.scope);

  // when can I call this method?
  // it modifies the dom
  drawChart(data){
    js.context.callMethod("drawChart", [
      element.shadowRoot.querySelector("svg"),
      new js.JsObject.jsify(data)
    ]);
  }
}

                

最满意答案

实现NgShadowRootAware的onShadowRoot方法,如下所示:

//... class SleepGraph implements NgShadowRootAware { //... // This method is called when the dom is ready void onShadowRoot(ShadowRoot shadowRoot){ } }

已有类似的问题:

AngularDart NgComponent在控制器中使用事件侦听器 如何在Angular.Dart中以编程方式添加组件?

Implement NgShadowRootAware's onShadowRoot method, like this:

//... class SleepGraph implements NgShadowRootAware { //... // This method is called when the dom is ready void onShadowRoot(ShadowRoot shadowRoot){ } }

There are already similar questions:

AngularDart NgComponent using event listeners in the controller How to add a component programatically in Angular.Dart?

更多推荐