How to get a reference of the component "Cart" itself instead of using querySelector() in class Cart?
Also, I want to know is there anyway to access the variable #i from class Cart?
@Component(
selector: '[cart]',
templateUrl: 'cart.html')
class Cart {
handling(){
querySelector("div[cart]");
}
}
<div cart>
<ul>
<li *ngFor="#i of items.values">{{i}}</li>
</ul>
</div>
@Component(
selector: '[cart]',
templateUrl: 'cart.html')
class Cart implements AfterViewInit {
// as mentioned by @Chandermani
ElementRef _element;
Cart(this._element);
@ViewChildren('myLi') ElementRef myLis;
// or for a single element or just the first one
// @ViewChild('myLi') ElementRef myLi;
ngAfterViewInit() {
// not initialized before `ngAfterViewInit()`
print(myLis);
}
handling(){
querySelector("div[cart]");
}
}
<div cart>
<ul>
<li #myLi *ngFor="let i of items.values">{{i}}</li>
</ul>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With