我正在尝试将我的angular 1指令代码转换为angular 2,但它抛出了一个错误,指出ElementRef.find()不是一个函数。有没有其他方法可以达到同样的效果。若要在元素中查找元素,请执行以下操作。
角度1
link: function ($scope, $elem, attrs) {
var elem = $elem[0] table = $elem,
thead = table.find('thead'),
tbody = table.find('tbody'),
}角度2
constructor(el: ElementRef, renderer: Renderer) {
this.elem = el.nativeElement;
this.table = el,
this.thead = this.table.find('thead');
this.tbody = this.table.find('tbody');
this.tableWidth = this.table[0].getBoundingClientRect().width;
}发布于 2017-02-04 21:34:07
构造函数(私有el: ElementRef,私有渲染器:渲染器) {}
//ngAfterContentInit() { // for directive or projected content
ngAfterViewInit() { // for searching a components template
this.elem = el.nativeElement;
this.table = el,
this.thead = this.table.querySelector('thead');
this.tbody = this.table.querySelector('tbody');
this.tableWidth = this.table[0].getBoundingClientRect().width;
}find是jQuery。Angular2中不包含jQuery。
另请参阅angular 2 / typescript : get hold of an element in the template
https://stackoverflow.com/questions/42040980
复制相似问题