javascript - NaN 的 typescript 错误

标签 javascript typescript

很抱歉问类似的问题,但我真的找不到出路。我被这个计算困住了。基本上,参数 a 到 g 是来自 HTML 的输入参数的值。我想用它来计算一定的总和。我尝试 console.log(+this.a) 它给了我一个数字。但是当我尝试 console.log(this.aa) 时,它给了我 NaN 。我可以知道如何解决这个问题吗?谢谢。

来 self 的 typescript 文件

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

}

来 self 的 HTML

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

最佳答案

ag 未定义,因此您必须在计算 aabb 之前获取参数值。 ..:

 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;

}

关于javascript - NaN 的 typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47206092/

相关文章:

typescript - 如何从类本身访问类实例成员和方法

javascript - 如何将 JavaScript 结果从本地文件发送到 VBA Webbrowser 控件

javascript - 在 React 中循环遍历 Map

javascript - 使用(PHP)从 Mysql 获取数据并使用 JSON 将其发送到 typescript/javascript

angular - 根据其他表单控件更新 Angular Reactive Form 禁用字段的值

angular - 如何从 Angular 2 中的 url 获取值

javascript - 在 Javascript 对象中显示字典

javascript - For循环不创建Div

javascript - HTML 工作表中的多个脚本之一不起作用

javascript - 如何使用 jQuery 选择所有文本区域和文本框?