[英]Select multiple dom elements in angular2
我試圖從組件模板中的 ul 中獲取多個 li。 對於一個元素,我可以使用 ElementRef.nativeElement.querySelector('selector') 但是如果我想定位具有特定類的多個元素,我會遇到麻煩。
我的模板:
<ul class="js-side-nav-content side-nav__content">
<li routerLink="/home">Home</li>
<li routerLink="/create">Create</li>
<li routerLink="/office">Office</li>
</ul>
組件代碼:
sideNavContentEl: HTMLElement;
constructor(private el: ElementRef) {}
this.sideNavContentEl = this.el.nativeElement.querySelector('.js-side-nav-content');
在這里,我選擇了<ul>
但我想獲得<li>
的。
我試過 querySelectorAll 但沒有得到正確的解決方案。 有沒有辦法使用 ElementRef 來實現這一點,還是我需要采取不同的方法?
像下面這樣的querySelectorAll
應該可以工作,
this.el.nativeElement.querySelectorAll('.js-side-nav-content > li');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.