Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit ea42c1e

Browse files
committed
refactor(): create $mdUtil.fakeNgModel() for when no ngModel supplied
1 parent f11e259 commit ea42c1e

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/components/checkbox/checkbox.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ angular.module('material.components.checkbox', [
4646
* </hljs>
4747
*
4848
*/
49-
function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant, $mdTheming) {
49+
function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant, $mdTheming, $mdUtil) {
5050
inputDirective = inputDirective[0];
51-
5251
var CHECKED_CSS = 'md-checked';
5352

5453
return {
@@ -74,18 +73,10 @@ function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant,
7473
tElement.attr('role', tAttrs.type);
7574

7675
return function postLink(scope, element, attr, ngModelCtrl) {
76+
ngModelCtrl = ngModelCtrl || $mdUtil.fakeNgModel();
7777
var checked = false;
7878
$mdTheming(element);
7979

80-
// Create a mock ngModel if the user doesn't provide one
81-
ngModelCtrl = ngModelCtrl || {
82-
$setViewValue: function(value) {
83-
this.$viewValue = value;
84-
},
85-
$parsers: [],
86-
$formatters: []
87-
};
88-
8980
$mdAria.expectWithText(tElement, 'aria-label');
9081

9182
// Reuse the original input[type=checkbox] directive from Angular core.

src/components/radioButton/radioButton.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ function mdRadioGroupDirective($mdUtil, $mdConstant, $mdTheming) {
6161

6262
function linkRadioGroup(scope, element, attr, ctrls) {
6363
$mdTheming(element);
64-
var rgCtrl = ctrls[0],
65-
ngModelCtrl = ctrls[1] || {
66-
$setViewValue: angular.noop
67-
};
64+
var rgCtrl = ctrls[0];
65+
var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
6866

6967
function keydownListener(ev) {
7068
if (ev.keyCode === $mdConstant.KEY_CODE.LEFT_ARROW || ev.keyCode === $mdConstant.KEY_CODE.UP_ARROW) {

src/core/util/util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ angular.module('material.core')
2323
*/
2424
iterator: iterator,
2525

26+
fakeNgModel: function() {
27+
return {
28+
$setViewValue: function(value) {
29+
this.$viewValue = value;
30+
this.$render(value);
31+
},
32+
$parsers: [],
33+
$formatters: [],
34+
$render: angular.noop
35+
};
36+
},
37+
2638
/**
2739
* @see cacheFactory below
2840
*/

0 commit comments

Comments
 (0)