【问题标题】:How to call method once instead some times?如何调用一次方法而不是几次?
【发布时间】:2020-01-11 10:00:15
【问题描述】:

我在 Angular 中有以下结构 DOM:

<div
      class="column"
      *ngFor="let cols of numberReturn(cols); let c = index"
      (click)="select(getMatrix(r, c))"
      title="{{ getMatrix(r, c).title }}"
      [ngClass]="{ active: isToolSelected(getMatrix(r, c)) }"></div>

如你所见,我有时会打电话给getMatrix(r, c)

如何调用一次并将结果传递到任何地方,例如:

let r = getMatrix(r, c);
(click)="select(r)"
title="{{ r.title }}"

【问题讨论】:

  • 你的问题不清楚

标签: angular angular7 angular8


【解决方案1】:

您可以将getMatrix(r, c) 的值与numberReturn 内的cols 数组的每个对象附加并通过键访问,例如您的numberReturn 看起来像

numberReturn(cols){
var retVal=[];//array your return here
retVal.forEach((item,index)=>{
item['matrixVal']=this.getMatrix(this.r,index);
})
}

然后在用户界面上

<div
      class="column"
      *ngFor="let cols of numberReturn(cols); let c = index"
      (click)="select(cols.matrixVal)"
      title="{{ cols.matrixVal.title }}"
      [ngClass]="{ active: isToolSelected(cols.matrixVal) }"></div>

【讨论】:

    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 2017-05-08
    • 2018-05-14
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    相关资源
    最近更新 更多