【问题标题】:Is it possible to increment a component property value inside *ngFor?是否可以在 *ngFor 中增加组件属性值?
【发布时间】:2020-06-08 05:27:21
【问题描述】:

我想在 ngFor 中为每次迭代增加一个类属性。如果有办法,请告诉我。提前致谢。

组件类:

class AA
   {      
  property:number = 0;
   }

模板:

 <div *ngFor = "let sample of samples">
    //increment property here
 </div>

【问题讨论】:

    标签: javascript angular


    【解决方案1】:

    您可以使用index

     <div *ngFor = "let sample of samples;let i = index">
        <span [class]="'opacity-'+(i+1)">value {{i+1}}</span>
        <button (click)="property = (i+1)">Set Selected Item</button>
     </div>
    Selected Item : {{property}}
    

    【讨论】:

    • 属性值如何在 div 中递增?
    • 你能举个像ngClass这样的例子吗??
    • 我只想增加属性值。但不涉及 ngClass 和其他东西。谢谢
    • 什么样的价值?
    • 它只是一个类属性值..say x : number = 0;
    【解决方案2】:

    只需在您的类中创建一个函数并将其调用到 ngfor bloc 中

    class AA
       {      
      property:number = 0;
      
      function increment_property(): void{
        this.property++;
      }
       }

    <div *ngFor = "let sample of samples">
        {{ increment_property() }}
     </div>

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 2022-06-29
      相关资源
      最近更新 更多