2017-05-11 2 views
0

이온 2를 가진 모바일 응용 프로그램을 개발 중이며, 물론 각 경주 테이블에 대해 1 년당 할당량을 계산해야합니다. 이 코드로 시도했지만 작동하지 않습니다. 도와 줄수있으세요?각도 2와 이온 2의 원소 합계 계산

<ion-row *ngFor="let ch of cheval1 "> 
     {{ch[0].annee }} 
    <div *ngFor="let m of members ; let rowIndex = index"> 
      <ion-col *ngIf="ch[0].annee == (m.Course.date |date : 'yyyy')"> 
      {{ m.Course.allocation}} 
      </ion-col> 
    </div> 

구성 요소

@Component({ 
    selector: 'page-view-cheval', 
    templateUrl: 'view-cheval.html', 
}) 
export class ViewChevalPage { 
    cheval ; 
    crs = 0 ; 
    cheval1 ; 
    members ; 
    vcheval : string ="Compteurs"; 
    constructor(public navCtrl: NavController, public navParams: NavParams, public data: ServiceProvider ,public menuCtrl: MenuController) { 
     this.cheval = navParams.data.member; 

     this.getIdCheval() ; 
    } 
    getIdCheval() { 
     return this.data.viewCheval(this.cheval.Cheval.id) 
     .subscribe(
        data=> { 
         this.members = data.course_cheval; 
         this.cheval1 =data.engagements; 
         console.log(this.members[1].Entraineur.nom); 
          console.log(data); 
        }, 
        err=> console.log(err) 
      ); 
    } 

FICHE의 JSON :

Course: { 
    id: "460", 
    date: "2012-06-24", 
    nom_du_prix: "GODOLPHIN ARABIAN", 
    allocation: "20000", 
    hippodrome_id: "2", 
    jouree: "36", 
    categorie_id: "1", 
    distance: "1600", 
     }, 
    Course: { 
    id: "306", 
    date: "2013-02-17", 
    nom_du_prix: "HAMADI BEN AMMAR", 
    allocation: "12000", 
    hippodrome_id: "2", 
    jouree: "10", 
    categorie_id: "2", 
    distance: "1600", 
    }, 
    Course: { 
    id: "328", 
    date: "2013-03-31", 
    nom_du_prix: "DE L’ INDÉPENDANCE", 
    allocation: "25000", 
    hippodrome_id: "2", 
    jouree: "19", 
    categorie_id: "1", 
    distance: "2000", 
    }, 
    engagements: [ 
    [ 
    { 
    annee: "2015" 
    } 
    ], 
    [ 
    { 
    annee: "2014" 
    } 
    ], 
    [ 
    { 
    annee: "2013" 
    } 
    ], 
    [ 
    { 
    annee: "2012" 
    } 
    ] 
    ] 

답변

3

당신은 구성 요소에 작업을 수행 할 수 있습니다

allocationSum: number; 
// other variables 

getIdCheval() { 
    // ... 
    this.members = data.course_cheval; 
    this.allocationSum = this.members.reduce((previous, current) => { 
    return previous + parseInt(current.Course.allocation); 
    }, 0); 
} 

또는 파이프 생성 :

0123을
@Pipe({ 
    name: 'sumByAllocation' 
}) 
export class SumByAllocationPipe implements PipeTransform { 
    transform(input: any): number { 
    if (Array.isArray(input)) { 
     return input.reduce((previous, current) => { 
     return previous + parseInt(current.Course.allocation); 
     }, 0); 
    } 

    return input;   
    } 
} 

그리고 템플릿: 답장을 보내

<div> Total Allocation: {{members | sumByAllocation}} </div> 
+0

먼저 덕분에, 나는 첫 번째 솔루션을 시도했지만 작동하지 않습니다 : 0 :에 런타임 오류 오류 0으로가 발생 : 속성을 읽을 수 없습니다 ' reduce 'of undefined –

+1

이봐, 난 그냥 "this"* keyword *를 놓쳤다. 나는 또한 당신의'members' 배열 *이 비동기 적으로 채워지는 것을 보았습니다, 그렇죠? 따라서이 * sum *은 내용이'this.members = data.course_cheval' 인 줄 다음에 채워 져야합니다 (이 경우). 편집 된 버전을 확인하십시오. – developer033

+0

이 솔루션을 사용하면 모든 허용량을 계산할 수 있지만 연간 할당을 계산해야합니다. –

관련 문제