Angular2 - hook methods

the life cycle of the component events

#Very Useful

  • ngOnChanges - called when an input or output binding value changes
  • ngOnInit - after the first ngOnChanges
  • ngDoCheck - developer’s custom change detection
  • ngAfterContentInit - after component content initialized
  • ngAfterContentChecked - after every check of component content
  • ngAfterViewInit - after component’s view(s) are initialized
  • ngAfterViewChecked - after every check of a component’s view(s)
  • ngOnDestroy - just before the directive is destroyed.

这里用了一个例子
ngAfterViewChecked

因为这个画图的方法是要定位一个元素

var vis = d3.select("#visualisation_today")

但这个元素是在
里面,即不是上来就能找到此元素
只有在条件成立时才会出现在DOM里
所以此时 用ngAfterViewInit 是不行了。要用check

https://github.com/dreambo8563/node_angular2/blob/ng-pages/app/components/weather.ts