如何获取ID列表以逗号分隔的字符串对象的角度

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

我跌倒了

 userList =[ { id: "0",name: "ALL"},{id: "1",name: "Smith"},{id: "2",name: "Smith"},{
 id: "3", name: "RAM"}];

当选择ALL选项时,我需要将用户的所有ID放入字符串对象。

字符串str = {1,2,3}(不包括0)

 <mat-select placeholder="User"(ngModelChange)="modelChanged($event)" formControlName="user" name="user">
  <mat-option *ngFor="let data of userList" [value]="data.id">{{data.name}}  </mat-option>
  </mat-select>

我的T​​ypescrpt:

 onChangeRole(): void {
        this.userRoleService.getUserDetails().subscribe((response: Response) => {
            if (response.failure) {
                this.modalPopupService.openPopup(AlertComponent, { message: response.error });
            } else {
                this.userList = response.result;
                const all = {
                    id:  0,
                    name:  'ALL'
                   };
                   this.userList.splice(0, 0, all);
            }
        });
    }

modelChanged(res): void {
        const userType = res;
        if (userType === 0) {
         this.userType = ???;
        } else {
            this.userType = userType;
        }
    }

在this.usertype之上是任何属性。

如何将ID列表转换为字符串对象?

arrays filter angular-material angular7 dropdown
1个回答
0
投票

从用户列表对象中删除全部属性,现在您的用户列表具有以下内容userList =[{id: "1",name: "Smith"},{id: "2",name: "Smith"},{ id: "3", name: "RAM"}];

<mat-select [(ngModel)]="selectedOption" (change)="optionChangeListener()">
    <mat-option value="All">All</mat-option>
    <mat-option *ngFor="let user of userList" [value]="user.name">
            {{ user.name }}
    </mat-option>
</mat-select>

在相应的.ts上添加

optionChangeListener():void{
    var a = [];
    for(var i=0;i<userList.length;i++){
        a.push(userList[i].id)}
    }

如果您想将其转换为字符串,只需执行a.toString()

© www.soinside.com 2019 - 2024. All rights reserved.