angular - 如何在 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}}
这工作正常,它正在打印所有内容,但我想在我视图的不同位置使用此对象内的值。
解决方案
访问使用[key]
如下,
{{account['accountId']}}
推荐阅读
- facebook - 访问 Facebook 的信息和广告
- javascript - getderivedstate 函数使状态不可编辑
- swift - 根据表格的可读宽度缩放 UITableViewCell 的高度
- sql-server - LAG 函数能否返回一个非 NULL 的 DATE 值
- python-3.x - Python 3 插入问题
- git - Git 不能正确检测某些文件的修改?
- excel - VBA 返回不正确的记录集
- javascript - 十六进制格式可以与 JSON 文件一起使用吗?如果是这样,怎么做?
- ms-access - 将 Access 2000 转换为 Access 365
- android - 共享首选项在 android studio 项目中被粉碎