简体   繁体   English

如何获得单个Angular组件的可寻址性?

[英]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.

相关问题 如何访问json响应的各个组件-Angular - How do I access individual components of json response - Angular 如何使用 Angular 从多个对象的列表中获取单个值 - How to get the individual value from a list of multiple objects using Angular 如何在服务器端渲染单个组件? - How to server side render individual components in react? 如何使用单个实现的CSS组件 - How to use individual materialize css components Angular - 如何在加载组件之前等待提供者获取请求完成? - Angular - How to wait for provider get request to finish before loading components? 如何从角度可观察的json文件中获取组件? - How to get the components from a json file from an observable in angular? Angular2:获取子组件的数量 - 在生命周期中的时间以及如何 - Angular2: get the count of child components - when in the lifecycle and how 如何使用JavaScript / Angular.js从数组中获取单个值 - How to get individual value from array using JavaScript/Angular.js 如何呈现多个组件并在react-native中设置单个状态? - How to render multi components and set individual status in react-native? 如何将 web 组件分离到单个文件并加载它们? - How to separate web components to individual files and load them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM