首页 > 解决方案 > 如何在 View Angular 6 中按键访问 json 对象值

问题描述

我是 Angular 6 的新手。我不知道如何使用键访问 Json 对象值。

我在我的服务中制作 http.get(url) 并在我的组件中调用该方法

export class CustomersComponent implements OnInit {

constructor(private accounts: AccountService) { }
account: Object;

ngOnInit() {
  this.accounts.getAccountById(1).subscribe((data)=> {
  this.account = data ;
}
);}}

返回的数据是一个json对象是这样的

{
 "accountId": 1,
 "attributeCode": "2300000000",
 "attributeLabel": "Customer Status"
 }

如何在视图中访问此 json 对象内的值?

我试过使用

{{account.accountId}}

在控制台中出现错误,因为 accountId 未定义

   {{account | json}}

这工作正常,它正在打印所有内容,但我想在我视图的不同位置使用此对象内的值。

标签: angular

解决方案


访问使用[key]如下,

{{account['accountId']}}

推荐阅读