小编Vil*_*llm的帖子

Angular 如何在 html 中显示数组中的单个对象

我正在尝试根据属性值显示数组中的单个对象。

我的交易项目列表有一个 accountId 属性,但我想改为显示帐户名称。所有帐户都加载到accounts$ 数组中。我就是不知道如何正确使用我的 getAccountById 函数

这是组件类

export class NewtransactionComponent implements OnInit {
  transaction$: Transaction;
  tempItem$: TransactionItem;
  accounts$: Array<Account>;

  constructor(private data: DataService) { }

  ngOnInit() {
    this.transaction$ = new Transaction();
    this.data.getAccounts().subscribe(data => this.accounts$ = Object.assign(new Array<Account>(), data));
    this.tempItem$ = new TransactionItem();
    this.transaction$.TransactionDate = new Date();
  }

  addItem(){
    this.transaction$.TransactionItems.push(this.tempItem$);
    this.tempItem$ = new TransactionItem();
  }

  getAccountById(id):Account{
    return this.accounts$.find(x => x.id === id);
  };
Run Code Online (Sandbox Code Playgroud)

这是给出错误“无法读取未定义的属性'名称'”的html视图

export class NewtransactionComponent implements OnInit {
  transaction$: Transaction;
  tempItem$: TransactionItem;
  accounts$: Array<Account>;

  constructor(private data: DataService) { } …
Run Code Online (Sandbox Code Playgroud)

html angular

2
推荐指数
1
解决办法
5005
查看次数

标签 统计

angular ×1

html ×1