简体   繁体   English

将变量从ngFor传递到组件的方法

[英]passing variable from ngFor to the component's method

What I want to achieve: 我要实现的目标:

Everytime the user selects some option on upper-select in the code below, I want to execute the component's method onSelectSampleType with the selected object as parameter which sets some public property of component to the passed parameter. 每次用户在下面的代码中选择上选择中的某个选项时,我都想使用所选对象作为参数执行组件的onSelectSampleType方法,该方法将组件的某些公共属性设置为传递的参数。

<select (change)="onSelectSampleType(SampleType)">
  <option  *ngFor="let SampleType of sampleTypeList">{{ SampleType }}</option>
</select>

<select *ngIf="selectedSample"">
  <option *ngFor ="let subType of selectedSampleType.subType">{{ subType }}</option>
</select>

Problem: the SampleType parameter in onSelectSampleType() method which is supposed to capture SampleType of ngFor directive is capturing nothing and passing undefined value. 问题: onSelectSampleType()方法中的应该捕获ngFor指令的SampleType的SampleType参数没有捕获任何内容并传递未定义的值。 I think the scope of SampleType variable inside ngfor directive is just limited to tag. 我认为ngfor指令中SampleType变量的范围仅限于标记。

Is there any way to accomplish what I want to achieve? 有什么方法可以实现我想要实现的目标吗?

Try this 尝试这个

<select (change)="onSelectSampleType(sel.value)" #sel>
  <option *ngFor="let SampleType of sampleTypeList; let i = index" [value]="i">{{ SampleType }}</option>
</select>

you should get index of selected option i think 您应该获得所选选项的索引,我认为

The problem with your code is that you're trying to access the SampleType variable outside of the scope of the ngFor directive. 代码的问题在于,您正在尝试访问ngFor指令范围之外的SampleType变量。 Since you're outside of the scope you can't access the variable, so your (change) callback doesn't receive the data you want. 由于您不在范围内,因此您无法访问该变量,因此您的(change)回调不会收到您想要的数据。

If the only purpose of your callback is to set the selectedSampleType property on your component, you can just use ngModel to bind the control to selectSampleType directly: 如果回调的唯一目的是在组件上设置selectedSampleType属性,则可以使用ngModel将控件直接绑定到selectSampleType

<select [(ngModel)]="selectedSampleType">
  <option  *ngFor="let SampleType of sampleTypeList">{{ SampleType }}</option>
</select>

If you require a more complex callback, you can use $event.target.value to get the value of the option which triggered the (change) event: 如果需要更复杂的回调,则可以使用$event.target.value获取触发(change)事件的选项的值:

<select (change)="onSelectSampleType($event.target.value)">
  <option  *ngFor="let SampleType of sampleTypeList">{{ SampleType }}</option>
</select>

You can find more information about Angular 2 event binding here: https://angular.io/docs/ts/latest/guide/template-syntax.html#!#event-binding 您可以在此处找到有关Angular 2事件绑定的更多信息: https ://angular.io/docs/ts/latest/guide/template-syntax.html#!#event-binding

You can pass the value as below: 您可以按如下所示传递值:

<select (change)="onSelectSampleType(selectedItem.value)" #selectedItem>
    <option *ngFor="let SampleType of sampleTypeList;let i=SampleType" [value]="SampleType">{{ SampleType }}</option>
</select>

TypeScript: 打字稿:

public sampleTypeList = ["SampleType1", "SampleType2", "SampleType3"];

public onSelectSampleType(sampleType: string) {
    console.log(sampleType);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM