javascript - AngularJS 上的单选框选定项目

标签 javascript angularjs radio-button

我想知道当我使用单选按钮列表时如何设置选中的项目

我的示例代码如下所示

<div ng-repeat="vehicle in filteredVehicle">
<input type="radio" name="radiog_lite" id="radio_{{$index}}" class="css-checkbox" ng-value="vehicle" ng-model="$parent.selectedVehicle" ng-disabled="vehicle.enable == '0'" />
<label for="radio_{{$index}}" class="css-label radGroup1" ng-if="vehicle.enable == '1'"></label>
<h5>{{vehicle.vehicleTypeName}}</h5>

下面我创建了plunker,请帮我修复所选项目 所选车辆位于 $scope.selectedVehicle

<强> Plunkr

谢谢

最佳答案

您应该将 selectedVehicle 与数组中的对象绑定(bind)。其背后的原因是 Angular 将 $$hashKey 添加到每个对象。

使用

$scope.selectedVehicle = {};
$scope.filteredVehicle = {};

$http.get('data.json').then(function(d){
    angular.forEach(d.data.vehicleType, function(value, key) {
          if(value.vehicleTypeID ==  d.data.selected.vehicleTypeID){
            $scope.selectedVehicle = value;
          }
    });
    $scope.filteredVehicle = d.data.vehicleType;
});

DEMO

关于javascript - AngularJS 上的单选框选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30567559/

相关文章:

javascript - 无法使用饼图 Highcharts 显示结果

javascript - javascript 中的参数作为全局变量

javascript - 通过与另一个数组进行比较来过滤 Angular 数组。

javascript - 自耕农 : jit-grunt error with grunt serve

C# 默认选中 GroupBox 中的 RadioButton

javascript - $ 在页面中阻碍 jquery 调用

javascript - 输入中带有 base64 编码图像的 POST 导致 Chrome 内存泄漏 - 如何解决?

javascript - 如何在 angular.js 中将 $filter 与 select 一起使用

javascript - 如何从 Angular Js中的html中的单选按钮获取选定的值

android RadioButton按钮可绘制重力