Added support for radio button directive. Fixes #97

This commit is contained in:
Max Lynch
2013-11-12 00:39:57 -06:00
parent 1627c242c4
commit d1e6f2e3ec
5 changed files with 185 additions and 9 deletions

View File

@ -0,0 +1,51 @@
(function(ionic) {
'use strict';
angular.module('ionic.ui.radio', [])
// The radio button is a radio powered element with only
// one possible selection in a set of options.
.directive('radio', function() {
return {
restrict: 'E',
replace: true,
require: '?ngModel',
scope: {
value: '@'
},
transclude: true,
template: '<label class="item item-radio">\
<input type="radio" name="group">\
<div class="item-content" ng-transclude>\
</div>\
<i class="radio-icon icon ion-checkmark"></i>\
</label>',
link: function($scope, $element, $attr, ngModel) {
var radio;
if(!ngModel) { return; }
radio = $element.children().eq(0);
if(!radio.length) { return; }
radio.bind('click', function(e) {
$scope.$apply(function() {
ngModel.$setViewValue($scope.$eval($attr.ngValue));
});
});
ngModel.$render = function() {
var val = $scope.$eval($attr.ngValue);
if(val === ngModel.$viewValue) {
radio.attr('checked', 'checked');
} else {
radio.removeAttr('checked');
}
};
}
};
});
})(window.ionic);

View File

@ -1,7 +1,10 @@
(function(ionic) {
'use strict';
angular.module('ionic.ui.toggle', [])
// The content directive is a core scrollable content area
// that is part of many View hierarchies
// The Toggle directive is a toggle switch that can be tapped to change
// its value
.directive('toggle', function() {
return {
restrict: 'E',
@ -39,3 +42,5 @@ angular.module('ionic.ui.toggle', [])
}
};
});
})(window.ionic);

View File

@ -20,6 +20,7 @@ angular.module('ionic.ui', [
'ionic.ui.list',
'ionic.ui.checkbox',
'ionic.ui.toggle',
'ionic.ui.radio'
]);
angular.module('ionic', [

View File

@ -0,0 +1,51 @@
<html ng-app="navTest">
<head>
<meta charset="utf-8">
<title>Radio</title>
<!-- Sets initial viewport load and disables zooming -->
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<link href="/vendor/font-awesome/css/font-awesome.css" rel="stylesheet">
<link rel="stylesheet" href="../../../../dist/css/ionic.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-touch.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-animate.js"></script>
</head>
<body>
<header class="bar bar-header bar-danger">
<h1 class="title">Beans</h1>
</header>
<content has-header="true"class="reveal-animation">
<div ng-controller="TestCtrl">
<form>
<radio ng-value="'magical'" ng-model="data.isLovely">Magical</radio>
<radio ng-value="'black'" ng-model="data.isLovely">Black</radio>
<radio ng-value="'pinto'" ng-model="data.isLovely">Pinto</radio>
<button ng-click="cycle()" class="button button-warning">Cycle</button>
<button ng-click="doIt()" class="button button-warning">Print</button>
</form>
</div>
</content>
<script src="../../../../dist/js/ionic.js"></script>
<script src="../../../../dist/js/ionic-angular.js"></script>
<script>
angular.module('navTest', ['ionic', 'ngAnimate', 'ngTouch'])
.controller('TestCtrl', function($scope) {
$scope.data = {};
var items = ['magical', 'black', 'pinto'];
var i = 0;
$scope.cycle = function() {
$scope.data.isLovely = items[i++ % items.length];
};
$scope.doIt = function() {
console.log('DOIT', $scope.data);
}
});
</script>
</body>
</html>