2017-08-01 47 views
1

我有以下对象:如何直接在角度4的模板绑定中增加变量?

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 
     } 
    ]; 

,我喜欢这样的循环这样的HTML:

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

我的问题是,是否有有一个局部变量,并通过30增加它的一种方式对于模板绑定中的ngfor中的每个元素?而不是有方法来增加?

,我有递增从方法变量的问题是,我得到以下错误:

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked

+0

你需要增加哪个数组的字段 –

+0

目前我只是想创建一个随机变量来增加。我不想在对象中添加一个字段并增加它。这只是为了说明我的问题。但是,我使用一个API,我得到的对象,所以我不能修改。 @sachilaranawaka – James

回答

3
<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/> 

通过这个你从0开始总长度的指标。

+0

因此对于3个项目的集合,上述返回'0,30,60'。我可以接受30,60,90吗? – James

+0

顺便说一句,当我打印时,变量返回对象内的当前元素和索引,这是错误的。 – James

+0

“变量”的类型是什么?尝试单独打印索引'i'并检查获得正确的值。你可以使用'* ngIf'跳过0 – Hareesh