如何调用方法一次而不是多次?
How to call method once instead some times?
我在 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 }}"
您可以将 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);
})
}
然后 ui
<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>
我在 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 }}"
您可以将 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);
})
}
然后 ui
<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>