简体   繁体   中英

Getting Child Component of tbody

I am not able to access Table Row of Data in Angular 2 Application,using Element Ref and query Selector.

ngAfterViewInit(){
            var e1 = this._elementRef.nativeElement.querySelector("tbody");
                        console.log(e1);
                e1.draggable="true";
    }

Above Code shows Data with tbody and child tr.

However,when I use

ngAfterViewInit(){
            var e1 = this._elementRef.nativeElement.querySelectorAll("tbody tr");
                        console.log(e1);
                e1.draggable="true";
    }

The Result is Coming As Null or Blank Array.[]

There is difference between querySelector and querySelectorAll .

Where querySelector works for you is because it returns DOM ( as you said data was returned ).

But with querySelctorAll you will get Node list. So, try to iterate trough that list and you will get table rows.

e1.forEach(tr => console.log(tr));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM