Skip to content

Instantly share code, notes, and snippets.

@miladvafaeifard
Created March 17, 2019 21:50
Show Gist options
  • Save miladvafaeifard/e90cf69dacbb7862ffc55fe4cb7c452c to your computer and use it in GitHub Desktop.
Save miladvafaeifard/e90cf69dacbb7862ffc55fe4cb7c452c to your computer and use it in GitHub Desktop.
Angular @ViewChildren
import { Component, OnInit, QueryList, ViewChildren, ElementRef} from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
@ViewChildren('foo') list: QueryList<ElementRef>;
items = [
{id: 1, name: 'coffee machine'},
{id: 2, name: 'refrigerator'},
{id: 3, name: 'toaster'}
]
ngAfterViewInit() {
this.list.toArray().forEach( (l: ElementRef) => {
console.log(l.nativeElement.dataset.index);
});
}
}
import { Component, OnInit} from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
items = [
{id: 1, name: 'milad'},
{id: 2, name: 'Alexandra'},
{id: 3, name: 'Sara'}
]
ngAfterViewInit() {
const list: NodeListOf<HTMLElement> = document.querySelectorAll('#foo');
list.forEach( i => console.log(i.dataset.index));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment