@@ -265,17 +265,32 @@ class NgModel extends NgControl implements NgAttachAware {
265
265
/**
266
266
* Usage:
267
267
*
268
- * <input type="checkbox" ng-model="flag">
268
+ * <input type="checkbox"
269
+ * ng-model="expr"
270
+ * [ng-true-value="t_expr"]
271
+ * [ng-false-value="f_expr"]
272
+ * >
269
273
*
270
- * This creates a two way databinding between the boolean expression specified
271
- * in ng-model and the checkbox input element in the DOM. If the ng-model value
272
- * is falsy (i.e. one of `false` , `null` , and `0` ), then the checkbox is
273
- * unchecked. Otherwise, it is checked. Likewise, when the checkbox is checked,
274
- * the model value is set to true. When unchecked, it is set to false.
274
+ * This creates a two way databinding between the `ng-model` expression
275
+ * and the checkbox input element state.
276
+ *
277
+ * If the optional `ng-true-value` is absent then: if the model expression
278
+ * evaluates to true or to a nonzero [num] , then the checkbox is checked;
279
+ * otherwise, it is unchecked.
280
+ *
281
+ * If `ng-true-value="t_expr"` is present, then: if the model expression
282
+ * evaluates to the same value as `t_expr` then the checkbox is checked;
283
+ * otherwise, it is unchecked.
284
+ *
285
+ * When the checkbox is checked, the model is set to the value of `t_expr` if
286
+ * present, true otherwise. When unchecked, it is set to the value of
287
+ * `f_expr` if present, false otherwise.
288
+ *
289
+ * Also see [NgTrueValue] and [NgFalseValue] .
275
290
*/
276
291
@NgDirective (selector: 'input[type=checkbox][ng-model]' )
277
292
class InputCheckboxDirective {
278
- final dom.InputElement inputElement;
293
+ final dom.CheckboxInputElement inputElement;
279
294
final NgModel ngModel;
280
295
final NgTrueValue ngTrueValue;
281
296
final NgFalseValue ngFalseValue;
@@ -285,14 +300,13 @@ class InputCheckboxDirective {
285
300
this .scope, this .ngTrueValue, this .ngFalseValue) {
286
301
ngModel.render = (value) {
287
302
scope.rootScope.domWrite (() {
288
- inputElement.checked = ngTrueValue.isValue (inputElement, value);
303
+ inputElement.checked = ngTrueValue.isValue (value);
289
304
});
290
305
};
291
306
inputElement
292
- ..onChange.listen ((value ) {
307
+ ..onChange.listen ((_ ) {
293
308
ngModel.viewValue = inputElement.checked
294
- ? ngTrueValue.readValue (inputElement)
295
- : ngFalseValue.readValue (inputElement);
309
+ ? ngTrueValue.value : ngFalseValue.value;
296
310
})
297
311
..onBlur.listen ((e) {
298
312
ngModel.markAsTouched ();
@@ -473,44 +487,45 @@ class NgValue {
473
487
}
474
488
475
489
/**
476
- * `ng-true-value` allows you to select any expression to be set to
477
- * `ng-model` when checkbox is selected on `<input type="checkbox">` .
490
+ * Usage:
491
+ *
492
+ * <input type=checkbox
493
+ * ng-model=model
494
+ * [ng-true-value=expr]>
495
+ *
496
+ * The initial value of the expression bound to this directive is assigned to
497
+ * the model when the input is checked. Note that the expression can be of any
498
+ * type, not just [String] . Also see [InputCheckboxDirective] , [NgFalseValue] .
478
499
*/
479
- @NgDirective (selector: '[ng-true-value]' )
500
+ @NgDirective (selector: 'input[type=checkbox][ng-model] [ng-true-value]' )
480
501
class NgTrueValue {
481
502
final dom.Element element;
482
503
@NgOneWay ('ng-true-value' )
483
- var value;
484
-
485
- NgTrueValue (this .element);
504
+ var value = true ;
486
505
487
- readValue (dom.Element element) {
488
- assert (this .element == null || element == this .element);
489
- return this .element == null ? true : value;
490
- }
506
+ NgTrueValue ([this .element]);
491
507
492
- bool isValue (dom.Element element, value) {
493
- assert (this .element == null || element == this .element);
494
- return this .element == null ? toBool (value) : value == this .value;
495
- }
508
+ bool isValue (val) => element == null ? toBool (val) : val == value;
496
509
}
497
510
498
511
/**
499
- * `ng-false-value` allows you to select any expression to be set to
500
- * `ng-model` when checkbox is deselected <input type="checkbox">`.
512
+ * Usage:
513
+ *
514
+ * <input type=checkbox
515
+ * ng-model=model
516
+ * [ng-false-value=expr]>
517
+ *
518
+ * The initial value of the expression bound to this directive is assigned to
519
+ * the model when the input is unchecked. Note that the expression can be of any
520
+ * type, not just [String] . Also see [InputCheckboxDirective] , [NgTrueValue] .
501
521
*/
502
- @NgDirective (selector: '[ng-false-value]' )
522
+ @NgDirective (selector: 'input[type=checkbox][ng-model] [ng-false-value]' )
503
523
class NgFalseValue {
504
524
final dom.Element element;
505
525
@NgOneWay ('ng-false-value' )
506
- var value;
507
-
508
- NgFalseValue (this .element);
526
+ var value = false ;
509
527
510
- readValue (dom.Element element) {
511
- assert (this .element == null || element == this .element);
512
- return this .element == null ? false : value;
513
- }
528
+ NgFalseValue ([this .element]);
514
529
}
515
530
516
531
/**
0 commit comments