请找到plunker单选按钮。我期待当我选择单选按钮时,$scope.itemList
中的所选对象将被分配到selectedItemDetails
,但它没有发生。并且默认情况下,当页面加载时,我希望根据var tagNumTobeSelectedByDefault = 2;
选择默认单选按钮,即默认选择"Gety of House"
,我该怎么办?
我从列表中获取要选择的对象的索引,如下所示:
var indexObjectTobeSet = $scope.itemList.map(function(x) {
return x.tagNum;
}).indexOf(tagNumTobeSelectedByDefault);
但没有设置特定的单选按钮。
答案 0 :(得分:2)
您没有设置索引,您设置了selectedItemDetails
'您希望在$scope.itemList
数组中选择的实际对象的值。因此,
$scope.selectedItemDetails = $scope.itemList.filter(function(item) { return item.tagNum === tagNumTobeSelectedByDefault; })[0];
应该可以工作(只需记住在 $scope.itemList
定义之后将其设置为)。您甚至可以考虑将itemList
对象移动到服务或常量中。
答案 1 :(得分:1)
当您将selectedItemDetails
声明为空文字{}
时,您没有特定的绑定。声明ng-model
可以附加到的属性:
$scope.selectedItemDetails = { selected : null }
和
<input type="radio" ng-model="selectedItemDetails.selected" name="eachCat" data-ng-value="eachCat">
然后它有效。现在,您还可以使用
设置默认选定的广播项目$scope.selectedItemDetails = { selected : $scope.itemList[3] }