2015-08-21 30 views
1

HTML:在角度js的無線電輸入中數據沒有與ng-repeat綁定?

<div ng-app="myApp" ng-controller="myCtrl">  
    <label ng-repeat="x in list"> 
    <input type="radio" ng-model="test" value="x" >{{x}} 
    </label> 
    <br> 
    Exec type: {{test}} 
</div> 

JS:

var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope) { 
$scope.list = ["xxx", "yyyy"]; 
    $scope.test = ""; 
}); 

JS小提琴:http://jsfiddle.net/0ku4jvsn/5/

沒有綁定了$scope.test。此行:Exec type: {{test}}什麼也沒有顯示。

回答

3

你應該使用$parent.test當你綁定你ng-model,因爲你是在一個ng-repeat

<label ng-repeat="x in list"> 
    <input type="radio" ng-model="$parent.test" value="{{ x }}" >{{x}} 
</label> 

要選擇默認值,只需用你想要的值賦給模型

var app = angular.module('myApp', []); 

app.controller('myCtrl', function($scope) { 
    $scope.list = ["xxx", "yyyy"]; 
    $scope.test = $scope.list[0]; 
}); 

https://jsfiddle.net/0ku4jvsn/6/

+0

默認情況下,我怎樣才能讓「xxx」選中? –

+0

我編輯我的答案有一個默認的選定值 –