I am using Angular 2.
When I have only one element, I can use nativeElement
first and then use Observable
.
<div #aaa></div>
@ViewChild('aaa') private aaa: ElementRef;
ngAfterViewInit() {
console.log(Observable.fromEvent(this.aaa.nativeElement), 'mouseenter');
}
But now I have a list, I cannot use same way to do it:
<a *ngFor="#user of users" #aaa (mouseenter)="mouseEnter($event, user)">
{{user}}
</a>
@ViewChild('aaa') private aaa: ElementRef;
ngAfterViewInit() {
console.log(Observable.fromEvent(this.aaa.nativeElement), 'mouseenter');
}
It will show the error:
EXCEPTION: TypeError: Cannot read property 'nativeElement' of null
When the mouses move into each element, I want it does different things using Observable. How can I correctly use Observable in this case?