是否热衷于从角度4的下拉列表中获取ID和名称?

时间:2018-12-02 04:41:01

标签: angular dropdown

在角度4中 .ts我想同时使用下拉菜单中的accounts.Id和accounts.Name。有可能吗?

<select [(ngModel)]="selectedName" class="form-control" (change)="SearchLedgerTransaction($event.target.value)">
      <option value=""></option>
      <option *ngFor="let accounts of accountledger" [value]="accounts.Id && accounts.Name">
          {{accounts.Name}}
      </option>
  </select>
  <br />
  {{selectedName}} Account Ledger View

2 个答案:

答案 0 :(得分:1)

如果帐户列表很长,并且只想获取其ID和名称,则可以尝试以下方法:

     -180/pi()*sign(x*y)*atan((abs(y)-abs(x))/(abs(y)+abs(x)))

然后,您的 selectedName 模型值将变为,例如: {id:1,name:'Kristy'}

已经创建了一个Stackblitz Demo供您参考。


更新

1。)您可以选择分别指定[ngModel]和(ngModelChange)。

    <option *ngFor="let accounts of accountledger" 
            [ngValue]="{id: accounts.Id, name: accounts.Name}">  // Use ngValue for multi-select control. Setup the object value you want to assign to the 'selectedName' model
        {{accounts.Name}}
    </option>

在您的组件上-分解参数,因为当前事件值是从选择框[ngValue]中传递的{id:accounts.Id,name:account.Name}

<select [ngModel]="selectedName" 
        (ngModelChange)="onSelectName($event)"   // Call onSelectName function if select is actively changed
        class="form-control" >

    <option value=""></option>

    <option *ngFor="let accounts of accountledger" 
            [ngValue]="{ id: accounts.Id, name: accounts.Name }">  // Pass Id and Name 
         {{accounts.Name}}
    </option>
</select>

答案 1 :(得分:0)

您可以使用类似..

<select [(ngModel)]="selectedName" class="form-control" (change)="SearchLedgerTransaction($event.target.value)">
    <option value=""></option>
    <option *ngFor="let accounts of accountledger" [value]="accounts">
        {{accounts.Name}}
    </option>
</select>
<br />
{{selectedName.Id}}{{selectedName.Name}} Account Ledger View