角度JS数字输入和默认值

AngularJS number input and default value

本文关键字:默认值 数字输入 JS 角度      更新时间:2023-09-26

我对AngularJS和数字输入有一个奇怪的问题。默认值将填充一秒钟,然后默认数字消失。在FX和Chrome上试用过。

从服务器返回的数据是:

{
    "name":"Rodolfo Heller",
    "desc":"Et ullam autem iure. Facere non fuga sit. Dolorum reprehenderit voluptatem vero rem at in.",
    "sell":"44.44",
    "image":"xxxx",
    "id":"1",
    "quantity":"1"
}

我对数字输入也有ng-init="product.quantity=1",但数字 1 闪烁然后消失。

<input type='number' min="1" step="1" class='form-control' ng-model="product.quantity" ng-init="product.quantity=1">

知道为什么默认值消失了吗?

谢谢。

可能是

从服务器返回的数据类型的问题,

{
    desc: "Et ullam autem iure. Facere non fuga sit. Dolorum reprehenderit voluptatem vero rem at in."
    id: "1"
    image: "hidden"
    name: "Rodolfo Heller"
    quantity: "1"                   // String value for quantity
    sell: "44.44"
}

您的分配quantity number输入,但是服务器将quantity作为string发送,如果您cast数据类型以从服务器int,或者number输入更改为text它将起作用。

因此,如果您使用int那么数据应该是这样的,

{
    desc: "Et ullam autem iure. Facere non fuga sit. Dolorum reprehenderit voluptatem vero rem at in."
    id: "1"
    image: "hidden"
    name: "Rodolfo Heller"
    quantity: 1                   // int
    sell: "44.44"
}