javascript - Angularjs 单选按钮没有给出 json 中的默认值

标签 javascript html angularjs json

我正在我的项目中实现以下单选按钮。我正在使用 angularjs 技术。

一旦我在浏览器上加载页面,但值没有出现在 JSON 对象中。我已经在标签中将属性指定为“checked=”true”。

当我再次单击单选按钮时,json 对象就会带有值。

<input type="radio" ng-model="formModel.internationalClient" ng-value="1">Yes

<input type="radio" ng-model="formModel.internationalClient" ng-value="0" ng-checked="true">No 

单击单选按钮之前的 json 对象

{}

单击单选按钮后 { “国际客户”:0 }

所以,实际上我在浏览器上加载页面后想要该值。

在浏览器上打开页面后,我想要以下结果。 { “国际客户”:0 }

最佳答案

如果这是单个表单模型(不重复),只需在 Controller 中初始化:

$scope.formModel.internationalClient = $scope.formModel.internationalClient || 0;

它是“将 ..internationalClient 设置为自身,除非 ..internationalClient 为 null”的简写。这样,如果是国际客户端,并且返回了值,那么这个操作就是幂等的。

如果是ng-repeat等形式:

<form ng-init="formModel.internationalClient = 0">

引用:https://docs.angularjs.org/api/ng/directive/ngInit

关于javascript - Angularjs 单选按钮没有给出 json 中的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45487914/

相关文章:

android - 如何在 Android 的 Cordova 应用程序中显示 PDF 文件?

angularjs - Amazon S3浏览器直接上传唯一文件名

javascript - 如果我希望我的指令重复 DOM,为什么我应该使用未链接的克隆 DOM

angularjs - 需要使用 css 技巧将元素放在前面

javascript - morrisjs 折线图未显示第一个 xLabel

javascript - 如何将关闭事件绑定(bind)到 window.open()

javascript - 使用 javascript 或 jquery 显示 pst 或通用时间

php - 在 PHP 中使用 PDO 将 HTML 表单中的数据插入 MySQL

javascript - 使用 chrome 扩展修改加载页面的 HTML

html - 使用 DIV/css 复制可变宽度 TABLE 列行为