【问题标题】:Angular How to display a single object from an array in htmlAngular如何在html中显示数组中的单个对象
【发布时间】:2019-05-01 20:18:24
【问题描述】:

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

我的交易项列表有一个 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);
  };

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

     <div class="items-container">
        <mat-form-field>
          <input matInput placeholder="Amount" [(ngModel)]="tempItem$.Amount">
        </mat-form-field>
        <mat-form-field *ngIf="accounts$">
          <mat-select placeholder="Account" [(ngModel)]="tempItem$.AccountId">
            <mat-option *ngFor="let account of accounts$" [value]="account.id">{{account.name}}</mat-option>
          </mat-select>
        </mat-form-field>
        <mat-form-field>
          <mat-select placeholder="Credit/Debit" [(ngModel)]="tempItem$.CreditDebit">
            <mat-option value="Credit">Credit</mat-option>
            <mat-option value="Debit">Debit</mat-option>
          </mat-select>
        </mat-form-field>
        <button mat-mini-fab color="primary" (click)="addItem()">Add</button>
      </div>
      <table *ngIf="transaction$.TransactionItems.length">
        <tr>
          <th>Amount</th>
          <th>Account</th>
          <th>Credit/Debit</th>
        </tr>
        <tr *ngFor="let item of transaction$.TransactionItems">
          <th>{{item.Amount | currency}}</th>
          <th>{{getAccountById(item.AccoundId).name}}</th>
          <th>{{item.CreditDebit}}</th>
        </tr>
      </table>

这些是可供参考的帐户和事务项数据模型

export class Account {
    Id: string;
    Name: string;
    Category: string;
    SubCategory: string;
}

export class TransactionItem{
    Id: number;
    TransactionId:number;
    Accountid: string;
    Amount: number;
    CreditDebit: string;
}

【问题讨论】:

  • 为什么在变量名中使用美元符号?通常,这用于indicate it is an observable?此外,如果您包含minimal reproducible example,您将获得更好的帮助。最后,如果没有一些样本数据,很难回答。我们只会猜测,假设您的类型正确匹配您的数据结构。
  • 我是 Angular 新手,我认为 $ 是用于全局变量之类的。谢谢你让我知道,我现在修正了我的变量名。下次我会努力做一个更好的例子。我只是觉得我在语法上遗漏了一些基本的东西。

标签: html angular


【解决方案1】:

我假设错误在这里:{{account.name}}?

这很可能是时间问题。该页面将在从订阅中检索数据之前尝试显示。

解决问题的一种方法是使用*ngIf

 <div class="items-container" *ngIf="account">

这样页面在检索到数据之前不会尝试显示。

另一种选择是使用安全导航运算符:

{{account?.name}}

如果帐户为 null 或未定义,问号告诉 Angular 不要尝试读取 name 属性。

编辑:

如果这里有错误:{{getAccountById(item.AccountId).name}},那么它告诉您getAccountById(item.AccountId) 未定义或为空。您的一笔交易是否有可能没有账户?

更仔细地查看您的代码,JavaScript/TypeScript 区分大小写。因此,如果您使用以下方式声明 id:

Id: string;

(大写 I)

然后您无法使用以下方式访问它:

return this.accounts$.find(x => x.id === id);

(小写)

【讨论】:

  • 抱歉,错误出现在 {{getAccountById(item.AccoundId).name}} 行,第一个循环通过 accounts 数组工作正常。这是一个错字,应该是“AccountId”,但即使没有错字,我也会遇到同样的错误
  • 就是这样。太感谢了!显然我的休息服务将所有字段的第一个字母更改为小写。在我开始学习 Angular 之前,我一直在使用反序列化器,所以我从未见过返回的原始 json。
猜你喜欢
  • 1970-01-01
  • 2019-02-27
  • 2019-11-24
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 2018-09-24
  • 2021-06-15
  • 2015-10-21
相关资源
最近更新 更多