对象的number属性在Angular2中转换为字符串

时间:2019-03-10 12:24:18

标签: javascript angular

我有一个具有两个属性的模型,其中一个是数字。

export class Ingredient
{
    constructor(public name:string,public amount:number)
    {

    }
}

但是,每当我尝试增加实际连接的数量时,该数量就会增加。奇怪的是,当我尝试将parseInt设置为该值时,出现错误,我无法将数字传递给parseInt,但是typeof返回该属性的字符串。

addIngredient(ingredient: Ingredient)
{
    let ingredientInList=this.ingredientInList(ingredient);
    //alert(ingredientInList);
    if(ingredientInList!==false)
    {
       // alert(typeof(this.ingredients[ingredientInList].amount));

      this.ingredients[ingredientInList].amount=this.ingredients[ingredientInList].amount + ingredient.amount;
    }
    else
    {
        //alert(typeof(ingredient.amount));

        this.ingredients.push(ingredient);
    }
    this.ingredientsChanged.emit(this.ingredients.slice());
}

因此,当我运行上面的代码时,成分旁边的数量将串联到先前的数量(假设它已经找到了相似的成分)。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

请确保两个“金额”的类型均为arc4random_buf而非字符串。

否则,您可以使用下面的代码来解析数字-

number