Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS中的单选按钮绑定_Angularjs - Fatal编程技术网

AngularJS中的单选按钮绑定

AngularJS中的单选按钮绑定,angularjs,Angularjs,我有一组4个单选按钮,在页面加载时,我必须预选与ng模型数据匹配的单选按钮。我已在值标记和ng值标记中定义了要匹配的数据,但它不起作用 A<input type="radio" name="export" value="A" ng-model="item.exportsCat" /> B<input type="radio" name="export" value="B" ng-model="item.exportsCat" /> C<input type="rad

我有一组4个单选按钮,在页面加载时,我必须预选与ng模型数据匹配的单选按钮。我已在值标记和ng值标记中定义了要匹配的数据,但它不起作用

A<input type="radio" name="export" value="A" ng-model="item.exportsCat" />
B<input type="radio" name="export" value="B" ng-model="item.exportsCat" />
C<input type="radio" name="export" value="C" ng-model="item.exportsCat" />
D<input type="radio" name="export" value="D" ng-model="item.exportsCat" />
A
B
C
D
页面加载ng model=“item.exportsCat”的值为“C”。
所以我希望选择C单选按钮。

然后您必须在控制器中初始化变量,如:
$scope.item.exportsCat='C'

这对我来说很有用:

JS:

HTML:


A.
B
D
C
这是我的JSFIDLE,它起作用了:


您根本不必在输入字段中定义
属性。将其替换为
ng值
。然后检查
$scope.item.exportCat
是否设置为value
C
@Sage,
value
属性也有效。这不是问题。@Zanon,我没有说它无效,我说它不如使用
ng value
有用,因此在定义
ng value
时,使用
value
属性将无法实现anything@Rohan,您需要提供更多信息。正如您所描述的,您的示例运行良好。请参阅此工作示例,并提供显示问题的更新版本。
angular.module('myApp', [])
.controller('ControllerFirst',function($scope){
  $scope.item = {
      exportsCat: 'D'
  };
});
<div ng-controller="ControllerFirst">
   <input type="radio" name="export" value="A" ng-model="item.exportsCat" />A
            <input type="radio" name="export" value="B" ng-model="item.exportsCat" />B
            <input type="radio" name="export" value="D" ng-model="item.exportsCat" />D
            <input type="radio" name="export" value="C" ng-model="item.exportsCat" />C
</div>