2017-11-09 6 views
1

죄송합니다. 비슷한 질문을 하긴하지만 정말 나가는 길은 찾을 수 없습니다. 나는이 계산에 매달렸다. 기본적으로 매개 변수 a ~ g는 HTML의 입력 매개 변수 값입니다. 특정 합계를 계산할 때이 값을 사용하고 싶습니다. 나는 console.log (+ this.a)를 시도했고 그것은 나에게 숫자를 준다. 하지만 console.log (this.aa)를 시도하면 NaN이 표시됩니다. 내가 어떻게 해결할 수 있는지 알고 싶습니까? 고맙습니다. 내 HTML에서 내 타이프 스크립트 파일NaN의 타이프 스크립트 오류

import { Component } from '@angular/core'; 
import { IonicPage, NavController, NavParams } from 'ionic-angular'; 


@IonicPage() 
@Component({ 
    selector: 'page-song', 
    templateUrl: 'song.html', 
}) 
export class SongPage { 
    a:string;aa:number = +this.a * 2; 
    b:string;bb:number = +this.b * 7; 
    c:string;cc:number = +this.c * 6; 
    d:string;dd:number = +this.d * 5; 
    e:string;ee:number = +this.e * 4; 
    f:string;ff:number = +this.f * 3; 
    g:string;gg:number = +this.g * 2; 
    sumA:number = (this.aa+this.bb+this.cc+this.dd+this.ee+this.ff); 
    sumB:number = this.sumA%11; 
    sumC:number = 11-this.sumB; 

    char: string; 
    constructor(public navCtrl: NavController, public navParams: NavParams) { 
    } 
    cal() 
    { 
    console.log(this.aa); 
    if(this.sumC == 1) 
    { 
     this.char = 'A'; 
    } 
    else if(this.sumC == 2) 
    { 
     this.char = 'B'; 
    } 
    else if(this.sumC == 3) 
    { 
     this.char = 'C'; 
    }else if(this.sumC == 4) 
    { 
     this.char = 'D'; 
    }else if(this.sumC == 5) 
    { 
     this.char = 'E'; 
    }else if(this.sumC == 6) 
    { 
     this.char = 'F'; 
    }else if(this.sumC == 7) 
    { 
     this.char = 'G'; 
    }else if(this.sumC == 8) 
    { 
     this.char = 'H'; 
    } 
    else if(this.sumC == 9) 
    { 
     this.char = 'I'; 
    }else if(this.sumC == 10) 
    { 
     this.char = 'Z'; 
    }else if(this.sumC == 11 || 0) 
    { 
     this.char = 'J'; 
    }else 
    { 
     this.char = 'NaN'; 
    } 
    } 

} 

에서

g

<ion-input name="two" class="box" maxlength="1" [(ngModel)]="a"></ion-input> 
    <ion-input name="three" class="box" maxlength="1" [(ngModel)]="b"></ion-input> 
    <ion-input name="four" class="box" maxlength="1" [(ngModel)]="c"></ion-input> 
    <ion-input name="five" class="box" maxlength="1" [(ngModel)]="d"></ion-input> 
    <ion-input name="six" class="box" maxlength="1" [(ngModel)]="e"></ion-input> 
    <ion-input name="seven" class="box" maxlength="1" [(ngModel)]="f"></ion-input> 
    <ion-input name="eight" class="box" maxlength="1" [(ngModel)]="g"></ion-input> 
    <button ion-button round block (click)="cal()">Validate</button> 
    <h2>The character is: {{char}}</h2> 
+0

문제는 다음과 같습니다.'aa : number = + this .a * 2;''aa'가 항상'this.a', times 2 "의 숫자 값인지 확인하는 것은 아닙니다. 이것은 "aa의 시작 값을 this.a'의 현재 값과 동일하게 만듭니다. (이것은'undefined', times 2입니다.)'a','b' 값이있을 때 필드를 재 계산해야합니다. 등등 변경합니다. – JLRishe

+0

하지만 console.log (this.a)와 console.log (+ this.a)를 입력하면 입력 한 번호가 표시됩니다! – DevJo

+0

@DevJo 예, 'this.a'는 당신이 그것을주는 가치가 있습니다 .' this.aa'는'this.aa '의 값을 어떻게 든 갱신하지 않으면'this.a'에 대한 변경 사항을 반영하지 않습니다 .. – JLRishe

답변

0

aaa 또는 bb의 계산 전에 PARAMS 값을 얻을 그래서 정의되지 않습니다 ... :

ngOnInit(){ 
    this.a = // <-- add value 
    this.a = // <-- add value 
    //... 
    this.b = // <-- add value 

    this.aa = +this.a * 2; 
    this.bb = +this.b * 7; 
    this.cc = +this.c * 6; 
    this.dd = +this.d * 5; 
    this.ee = +this.e * 4; 
    this.ff = +this.f * 3; 
    this.gg = +this.g * 2; 

this.sumA = (this.aa+this.bb+this.cc+this.dd+this.ee+this.ff); 
this.sumB = this.sumA%11; 
this.sumC = 11-this.sumB; 

} 
+0

u는 맞다! 고마워요. – DevJo

+0

당신은 환영합니다. –