首页 > 解决方案 > Angular 6调用方法并将值分配给*ngFor中的变量

问题描述

我想在里面调用一个方法ngFor,想把返回值赋给一个局部变量。我尝试了这种方法:

<div *ngFor="let card of partNumberPlanningCards; let i = index" class="partnumber-basic-card">
<div 
  *ngFor="let scope of containerScopeLineItems; let lineItem = getOperatingWeightBy(scope.containerScopeId, card.typeId)">
   </div>
 </div>

但它显示了这个错误:

Parser Error: Unexpected token (, expected identifier, keyword, or string at column 74 in [let scope of containerScopeLineItems; let lineItem = getOperatingWeightBy(scope.containerScopeId, card.typeId)] in ng

标签: javascripthtmlangulartypescriptangular2-template

解决方案


您可以将function返回值存储在任何 of 中attributeelement并使用element引用来获取值。

<div *ngFor="let card of partNumberPlanningCards; let i = index" class="partnumber-basic-card">
    <div *ngFor="let scope of containerScopeLineItems" #lineItem [title]="getOperatingWeightBy(scope.containerScopeId, card.typeId)">

        {{lineItem.title}}

    </div>
</div>

此处title已使用,但您可以使用元素的任何有效属性。


推荐阅读