【发布时间】:2023-03-03 13:10:01
【问题描述】:
我从我的服务器收到一个对象列表,格式如下:
[ { 'id' : 1, ... }, { 'id' : 2, ... }, { 'id' : 3, ... } ]
我试图允许用户使用radio 按钮选择对象。这是以下 HTML 代码:
<input type="radio" ng-model="id" value={{ obj.id }} />
我正在为列表中的每个对象重复这段代码。问题是,当单击其中一个按钮时,会出现以下错误:
Error: ngModel:numfmt
Model is not of type `number`
每个对象的 ID 显然是一个数字,如下控制台日志所述:
console.log(typeof (obj.id)); <- return 'number'
但它仍然不起作用。谁能帮我发现为什么它坏了?
EDIT 遍历对象数组的代码:
<ul ng-repeat="obj in objectList>
<li>
<label><input type="radio" string-to-number ng-model="id" value={{ obj.id }} />
{{ obj.id }}</label>
</li>
</ul>
【问题讨论】:
-
没有。
numfmt指的是来自 Angular 错误自动生成页面的错误numerical format。它指出错误是ngModel错误,更准确地说是numerical format错误。这对我来说毫无意义,因为我在该控制器内部仔细检查了数据实际上是一个数字。 :( -
你能展示遍历对象数组的代码吗?
-
试试
ng-model="obj.id" -
字符串转数字有什么作用?
-
对不起,我把它取下来了,这是一个应该将字符串转换为数字的 dirrecty,但没有成功
标签: javascript angularjs string types integer