[英]How to get addressibility to individual Angular components?
Environment: Angular 7 using the ng-select component. 环境:使用ng-select组件的Angular 7。
Scenario: A *ngFor which creates a list of ng-select components as follows: 场景:一个* ngFor,它创建一个ng-select组件列表,如下所示:
<div *ngFor="let address of addresses">
<ng-select
[items]="address.cities"
bindLabel="name"
bindValue="id">
</ng-select>
</div>
The ng-select component has methods which I need to call. ng-select组件具有我需要调用的方法。 How do I get each instance of ngSelect instance in order to call those methods from the model side. 我如何获取ngSelect实例的每个实例,以便从模型端调用这些方法。
I am able to see the decomposed htmlelements in the DOM but I don't want to traverse the DOM I just want to call certain ng-select methods but don't know how to get an instance of the component from the model. 我能够在DOM中看到分解的htmlelements,但我不想遍历DOM,我只想调用某些ng-select方法,但不知道如何从模型中获取组件的实例。
An example of a method call would be: 方法调用的示例为:
instanceOfNgSelect.HandleClear();
I would want access to any of the instances created from the *ngFor... 我想访问从* ngFor ...创建的任何实例。
if I understood correctly, you want to call second one out of 4? 如果我理解正确,您想打出四分之一吗? if so, did you try initiating index when looping? 如果是这样,您是否在循环时尝试启动索引? *ngFor="auth of authors, let i = index;", that case you can pass the index and identify based on the index. * ngFor =“作者的身份验证,让i = index;”,这种情况下,您可以传递索引并根据索引进行标识。
Given: 鉴于:
<div *ngFor="let address of addresses">
<ng-select
[items]="address.cities"
bindLabel="name"
bindValue="id">
</ng-select>
</div>
The best way to get addressibility to any of the ng-selects above is in the Model using the ViewChildren decorator: 获得对上述任何ng-select的可寻址性的最佳方法是在模型中使用ViewChildren装饰器:
@ViewChildren(NgSelectComponent) NgSelectComponents: QueryList<NgSelectComponent>;
Then in ngAfterViewInit(): 然后在ngAfterViewInit()中:
this.NgSelectComponents.forEach(ngselect=>{
debugger;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.