如何获取组件“Cart”本身的引用而不是在类 Cart 中使用 querySelector()?
另外,我想知道是否可以从 Cart 类访问变量 #i?
@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>
Run Code Online (Sandbox Code Playgroud)
@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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7219 次 |
| 最近记录: |