如何提取用户通过Angular中的mat-options(下拉列表)选择的对象的多个属性?

问题描述 投票:1回答:1

TLDR:下拉列表是对象列表。不确定如何捕获所选对象的多个属性。

我目前正在做什么:

我有一个简单的下拉列表,显示通过API调用返回的obj的详细信息:

 <mat-form-field class="testSection">
        <mat-label>Available Test Results:</mat-label>
        <mat-select [(ngModel)]="testName" name="testName" ngDefaultControl>
          <mat-option *ngFor="let test of tests" [value]="test.testName">
            {{ test.testName + test.testType + test.results + test.cohortId}}
          </mat-option>
        </mat-select>
 </mat-form-field>

在我的component.ts上,我有一个testName定义为字符串:

testName: string

因此,我确实能够根据用户在下拉列表中选择的内容(基于用户单击的特定test.testName)来检索正确的test

我需要什么:

[我想进一步扩展它。可以说我想要[[both test.nametest.results并将它们分配给变量testNametestResults以用于进一步的API调用。我该怎么做?

我尝试将[value]="test.testName"更改为[value]="test.testName, test.testResults"[(ngModel)]="testMap",其中testMap在组件中定义为:testMap: Map<string, string>

不幸的是,它什么也没做,我最终只捕获了test.testName。

angular typescript angular-material dropdown
1个回答
1
投票
您可以简单地将您的[(ngModel)]变量绑定为整个测试对象。像这样:
© www.soinside.com 2019 - 2024. All rights reserved.