我试图将我的角度1指令代码转换为角度2.但它会抛出一个错误,说ElementRef.find()不是一个函数。有没有其他方法来实现同样的目标。在元素中找到元素。
Angular 1
link: function ($scope, $elem, attrs) {
var elem = $elem[0] table = $elem,
thead = table.find('thead'),
tbody = table.find('tbody'),
}
Angular 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;
}
答案 0 :(得分:2)
构造函数(private el:ElementRef,private renderer:Renderer){}
//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