官方文檔
在調用 NgAfterViewInit 回調函數以前就會設置這些視圖查詢。
元數據屬性:html
所支持的選擇器包括:api
代碼:app
<div #testBox [appElementTitle]="'屬性指令測試'" [appCopyAttr]="'https://liangyuetian.cn/'"> appElementTitle 屬性指令測試 appCopyAttr 屬性指令測試 </div> <div #box [appElementTitle]="'這是box的title'" [appCopyAttr]="'https://baidu.com.cn/'"> appElementTitle 屬性指令測試 appCopyAttr 屬性指令測試 </div>
export class AppComponent implements OnInit, AfterViewInit { @ViewChild('testBox', {read: ElementRef}) elBox: ElementRef; @ViewChild('testBox', {read: CopyAttrDirective}) copy: CopyAttrDirective; @ViewChild('testBox', {read: ElementTitleDirective}) titles: ElementTitleDirective; @ViewChild('box', {read: ElementRef}) elBox2: ElementRef; @ViewChild('box', {read: CopyAttrDirective}) copy2: CopyAttrDirective; @ViewChild('box', {read: ElementTitleDirective}) titles2: ElementTitleDirective; keyUpSearch($event: { [key: string]: any }) { console.log($event.code, $event.key, $event); } ngOnInit() { } ngAfterViewInit() { console.log('one', this.elBox, this.copy, this.titles); console.log('tow', this.elBox2, this.copy2, this.titles2); } }
效果:
函數