我正在使用Laravel中的JSON响应,但返回不是数组。我要做什么?有人能指点吗?我搞错了,弄不明白为什么??下面是代码
拉勒维尔
public function show($id)
{
$arCategoria = \App\Favorito::join('categoria', 'categoria.cd_categoria', '=', 'link.cd_categoria')
->select('*')
->where('categoria.cd_categoria_pai',$id)
->where('link.cd_usuario',$this->token['cd_usuario'])
->where('link.bo_ativo',true)
->get();
$link = $this->processarCategoria($arCategoria);
return $link;
}
public function processarCategoria($arCategoria){
$ar = array();
$cont = 0;
foreach($arCategoria as $key => $value){
$ar[$value['no_categoria'].'_'.$value['cd_categoria']][] = array(
'no_link'=>$value['no_link'],
'cd_link'=>$value['cd_link'],
'vl_link'=>$value['vl_link'],
'bo_ativo'=>$value['bo_ativo'],
'link'=>$value['link']
);
$cont++;
}
return $ar;
}我返回的拉拉api
{
"Documentation_3": [
{
"no_link": "stackoverflow",
"cd_link": 5,
"vl_link": null,
"bo_ativo": 1,
"link": "https://stackoverflow.com"
},
{
"no_link": "Adventures of Time",
"cd_link": 9,
"vl_link": null,
"bo_ativo": 1,
"link": "http://adventuresoftime.com.br"
}
],
"Things to buy_5": [
{
"no_link": "Games",
"cd_link": 10,
"vl_link": null,
"bo_ativo": 1,
"link": "Games.com.br"
}
]
}还有我的service.ts
getLinksByIdusuario(id:number):Observable<any[]> {
return this.http.get<any[]>(`${API}/favorito/${id}`)
.pipe(map((data: any) => data ),
catchError(error => { return throwError(error)})
);
}component.ts
ngOnInit() {
this.id = params['id'];
this.homeService.getLinksByIdusuario(this.id)
.subscribe(
categorias => {
this.categorias = categorias,
console.log(this.categorias)
}
)
}component.html
<div class="row">
<div *ngFor="let categoria of categorias">
{{categoria |json}}
</div>
</div>

我应该改变我的拉拉后端还是有角的前部?如何解决这个问题?
发布于 2019-01-07 00:21:00
您正在尝试遍历一个对象,这在角2+ 4和5中是不可能的。
你们的一些选择:
1-请求后端将其转换为数组:
[
{
"key":"Documentation_3",
"value": [
{
"no_link": "stackoverflow",
"cd_link": 5,
"vl_link": null,
"bo_ativo": 1,
"link": "https://stackoverflow.com"
},
],
},
{
"key":"Things to buy_5",
"value" : [
{
"no_link": "Games",
"cd_link": 10,
"vl_link": null,
"bo_ativo": 1,
"link": "Games.com.br"
}
]
}
]2-您可以创建一个对象密钥管道,或者如果您有angular6+,则可以使用键值管道。
`<div *ngFor="let category of categorias | keyvalue"> {{category.key}}:{{category.value}} ===>> this is an array , so you need another loop here </div>`https://stackoverflow.com/questions/54066855
复制相似问题