#目的
[
{
"id": 1,
"description": "1 - Gulf Coast"
},
{
"id": 2,
"description": "10 - West Great Lakes"
},
{
"id": 3,
"description": "11 - California"
},
]
#html代碼
<mat-form-field appearance="fill" >
<mat-label>Region</mat-label>
<mat-select [(ngModel)]="territoryAssignmentFields.region" name="region">
<mat-option *ngFor="let region of regions" [value]="region.id">{{ region.description }}</mat-option>
</mat-select>
</mat-form-field>
我們如何在角度模板上決議 mat-select 選項的值將是來自描述物件的 id 例如基于下面的物件[value]應該是:
1、10、11。
它應該從描述中獲取 id 或數字,然后將其用作[value]mat-select 選項。
我們可以在模板上決議它嗎?謝謝。
#tscode
getregion() {
this.searchString = "";
this._pickListService.getregion(this.accountId,this.searchString).subscribe(
res => {
this.regions = res.data;
console.log(" this.regions", this.regions)
},
err => {
console.log('Filtered Territory Assignments Region Error')
console.log(err);
}
)
}
uj5u.com熱心網友回復:
在訂閱事件中,Array.map()生成一個帶有 id 的新陣列:
description按-符號分割值。- 從 (1) 中獲取第一個值。
- 從 (2) 修剪間距。
number從 (3)投射到型別。
this._pickListService
.getregion(this.accountId, this.searchString)
.subscribe(
(res) => {
let data: any[] = res.data.map((x: any) => {
return {
id: parseInt(x.description.split('-')[0].trim()),
description: x.description
};
});
this.regions = data;
console.log(' this.regions', this.regions);
},
(err) => {
console.log('Filtered Territory Assignments Region Error');
console.log(err);
}
);
StackBlitz 上的示例演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/395555.html
標籤:javascript 有角的 打字稿
