我需要用接口信息填充字符串数组。
我用以下代码发送服务器请求返回角色列表:
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')
);
是的。现在我需要用
selectedRole:string[];
填充这个变量。从服务器接收。
我该怎么做?
最佳答案
首先,this.claims
应该是一个声明数组,因为代码中的data
变量也是一个声明数组。
如果您在.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/