2017-08-01 7 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内の各要素について?増分を行うメソッドを持つよりもむしろ?あなたは、全体の長さに0から始まるインデックスを取得することにより、この

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

3項目のコレクションの場合、上記は '0、30、60'を返します。 '30、60、90'を受け取ることはできますか? – James

+0

btw、私は印刷すると、変数は、オブジェクト内の現在の要素と、間違っているインデックスを返します。 – James

+0

'variable'の型は何ですか?索引 'i'だけを印刷して正しい値を取得してみてください。あなたは '* ngIf'を使って0 – Hareesh

関連する問題