我需要用接口(interface)信息填充字符串数组。
我使用此代码发送服务器请求以返回 Angular 色列表:
public GetRoleClaim(id:number):Observable<string[]>{
return this.http.get<string[]>('https://localhost:44390/api/Role/GetRoleClaims/'+id,{headers: this.headers}).pipe(
tap(Claims => this.log("fetch claims")),
catchError(this.handleError('Error GetRoleClaim', []))
);
}
它工作正常。
在此之后,我使用以下代码提供来自组件的数据:
this.roleService.GetRoleClaim(this.roleId).subscribe((data)=>{
this.selectedRole=data
},
(error)=>
swal('خطا',`هنگام دریافت اطلاعات خطایی رخ داده . لطفا با پشتیبانی تماس بگیرید`,'error')
);
.现在我需要用 claimValue
填充这个变量 selectedRole:string[];
。
claimValue
从服务器接收。
我该怎么做?
最佳答案
首先,this.claims
应该是一个 Claims 数组,因为代码中的 data
变量也是一个 Claims 数组。
如果您在 .ts
文件中全局定义了“claims”变量定义,则它看起来像这样。
claims: Claims[] = [];
现在,您只需编写一个 for 循环来填充 selectedRole
变量。
for (const value of this.claims) {
selectedRole.push(value.claimValue);
}
假设 selectedRole 是在本地定义的。如果它是全局定义的,那么上面的代码将如下所示:
for (const value of this.claims) {
this.selectedRole.push(value.claimValue);
}
关于javascript - 用 Angular 6 中的接口(interface)信息填充字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53466071/