2015-09-10 123 views
1

默认情况下我应该选中一个单选按钮选项。 我试图给ng-modelng-init,但它不工作。如何使默认情况下选中一个单选按钮选项

我的HTML:

<div bindonce ng-repeat="row in results.favorites" style="padding: 0px 10px; white-space: nowrap; border-bottom: 1px solid #efefef; border-top: 1px solid #eee; border-left: 1px solid #efefef;"> 
    <input type="radio" ng-click="setFavoriteDashboard(row.id)" ng-model="selectedRow" name="favRadioSel" style="opacity: 1;" class="pointer"/> 
    <span bo-text="row.title" style="margin: 10px; position: relative; top: 3px;"></span> 
</div> 

enter image description here

我的JS:

$scope.results = { 
    favorites: [{ 
    id: "WatchList1", 
    title: "WatchList1" 
    }, { 
    id: "WatchList2", 
    title: "WatchList2" 
    }, { 
    id: "WatchList3", 
    title: "WatchList3" 
    }] 
}; 
$scope.selectedRow = "WatchList1"; 

演示:http://plnkr.co/edit/KA9HyQ3bHxJo2Cm9qFm3?p=preview

回答

1

检查工作演示:Plunker。使用ng-value="row.id"代替使用ng-value="{{row.id}}"

请查阅文档的ngValue

<input 
    [ng-value="string"]> 
... 
</input> 
+0

范围变量应在parent..rather比在矿井答案'NG-repeat'..look .. –

0

设置$scope.selectedRow$scope.results.favorites[0].id和使用value,而不是ng-value

它是如何工作value单选按钮被设置为idng-model设置为selectedRow,根据您的plunker。因此,如果你想使数组默认的第一个值,你只需指定selectedRowid

See plunker

0

您需要使用$parent指向父范围selectedRow变量ng-repeat并创造一个孩子范围,&还需要使用ng-value属性值,无插值。

标记

<input type="radio" 
ng-model="$parent.selectedRow" 
ng-value="row.id" name="favRadioSel" 
style="opacity: 1;" 
class="pointer" /> 

Working Plunkr

相关问题