Skip to content

Instantly share code, notes, and snippets.

@magnet88jp
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save magnet88jp/e0f99102bb5362c014a1 to your computer and use it in GitHub Desktop.

Select an option

Save magnet88jp/e0f99102bb5362c014a1 to your computer and use it in GitHub Desktop.
angular radio button default test
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.28/angular.js"></script>
<script>
var testApp = angular.module('app',[]);
testApp.controller('testCtrl', ['$scope', function($scope){
$scope.items = [
{id:100, name:"name1"},
{id:101, name:"name2"},
{id:102, name:"name3"},
{id:103, name:"name4"}
];
$scope.fixRadio = function() {
console.log('outtext=' + $scope.my.checked );
}
$scope.my = { checked: 'name3' };
}]);
</script>
</head>
<body ng-app="app">
<div ng-controller="testCtrl">
<ul>
<li ng-repeat="item in items">
<input type="radio" name="testitem" ng-model="my.checked" ng-value="item.name" />
<label>{{item.name}}</label>
</li>
</ul>
<div>You chose {{my.checked}}</div>
<div>
<button ng-click="fixRadio()">fix</button>
</div>
<h4>参考</h4>
* http://stackoverflow.com/questions/29539549/radio-button-checked-by-default-when-using-ng-repeat
* http://stackoverflow.com/questions/15326053/setting-the-default-value-for-a-radio-inside-of-ng-repeat-angular-js
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment