简体   繁体   English

如何在角度4中直接在模板绑定中增加变量?

[英]How to increment variable directly in template binding in angular 4?

I have the following object: 我有以下对象:

  this.people = [{
        name: 'Douglas  Pace',
        title: 'Parcelivery Nailed The Efficiency',
        content: 'Since I installed this app, its always help me book every tickets I need like flight, concert, ' +
          'movie or hotel. I don\'t need to install different app for different ticket. The payment is also very easy',
        image: '../../assets/img/profile_pics/profile_pic.jpg',
        rate: '4.5',
        classActive: 'testimonials__selected-visible',
        active: true
      },
      {
        name: 'Naseebullah  Ahmadi',
        title: 'Parcelivery Nailed The Efficiency',
        content: 'Since I installed this app, its always help me book every tickets I need like flight, concert, ' +
          'movie or hotel. I don\'t need to install different app for different ticket. The payment is also very easy',
        image: '../../assets/img/profile_pics/profile_pic.jpg',
        rate: '4.5',
        classActive: '',
        active: false
      },
      {
        name: 'Haseebullah Ahmadi',
        title: 'Parcelivery Nailed The Efficiency',
        content: 'Since I installed this app, its always help me book every tickets I need like flight, concert, ' +
          'movie or hotel. I don\'t need to install different app for different ticket. The payment is also very easy',
        image: '../../assets/img/profile_pics/profile_pic.jpg',
        rate: '4.5',
        classActive: '',
        active: false
      }
    ];

and I am looping this in html like so: 我在html中循环这样:

  <ng-template ngFor let-person="$implicit" let-variable [ngForOf]="people">
          {{variable + 30}}
   <ng-template/>

My question is, is there a way of having a local variable and incrementing it by 30 for each element inside the ngfor in template binding? 我的问题是,是否有一种方法可以为模板绑定中的ngfor中的每个元素设置局部变量并将其递增30? Rather than having methods to do the incrementing? 而不是有方法来增加?

The issue that I have with incrementing the variable from methods is that i get the following error: 我从方法增加变量的问题是我得到以下错误:

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改

<ng-template ngFor let-person="$implicit" let-variable [ngForOf]="people" let-i="index">
  <p *ngIf="i == 0">{{variable + 30}}</p>
  <p *ngIf="i > 0">{{variable + (30*i)}}</p>
<ng-template/>

By this you get the index starting from 0 to total length. 通过这样,您可以获得从0到总长度的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM