1
即時嘗試將我的角1指令代碼轉換爲角2.但它會拋出一個錯誤,指出ElementRef.find()不是函數。有沒有其他辦法可以達到同樣的效果。在元素中查找元素。ElementRef.find()不是函數angular 2
角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;
}
this.table裏面的angular 2指令docent顯示內部元素。所以querrySelector文件可以工作。甚至嘗試this.table.nativeElement.querySelector('thead') – janIreal23
請提供更多的代碼。從這些微小的代碼片段中診斷髮生了什麼是不可能的。如果HTML位於組件的模板中,那麼您還需要將代碼從構造函數移動到「ngAfterViewInit()」;如果代碼位於指令中或其投影內容更新了我的答案,則需要將代碼從構造函數移動到ngAfterViewInit 。 –
完美'this.table.nativeElement.querySelector('thead');''ngAfterViewInit()'內''工作。 – janIreal23