有了這個準備的鍵的匹配值,但對方的回答是我前遞交。您需要設置myObject.value
以匹配您想要作爲默認值的option.key
。
的script.js
(function() {
angular.module('radioExample', [])
.controller('ExampleController', ['$scope',
function($scope) {
$scope.options = [{
'key': 'Option 1',
'val': 'Value 1'
}, {
'key': 'Option 2',
'val': 'Value 2'
}, {
'key': 'Option 3',
'val': 'Value 3'
}];
$scope.myObject = {
value: 'Option 2'
};
}
]);
})();
的index.html
<!DOCTYPE html>
<html ng-app="radioExample">
<head>
<script data-require="[email protected]*" data-semver="1.4.0-beta.6" src="https://code.angularjs.org/1.4.0-beta.6/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="ExampleController">
<form name="Options">
<div ng-repeat="option in options">
<label>{{option.key}}</label>
<input type="radio" ng-model="myObject.value" ng-value="option.key">
</div>
Selected option is {{myObject.value}}
</form>
</body>
</html>
的[角JS NG-重複一個單選按鈕從列表範圍與默認選中](可能重複的http://計算器。 com/questions/25389701/angular-js-ng-repeat-a-radio-button-list-scope-with-a-default-checked) – 2015-03-24 23:25:16
@MarkoGrešak謝謝,但我不知道那個例子是怎麼回事無論是和解決方案不工作。 – Proto 2015-03-24 23:34:12