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

Confusing Type Coercion with ng-value #15283

Closed
jongunter opened this issue Oct 17, 2016 · 4 comments · Fixed by #15288
Closed

Confusing Type Coercion with ng-value #15283

jongunter opened this issue Oct 17, 2016 · 4 comments · Fixed by #15288

Comments

@jongunter
Copy link

What is the current behavior?
When a model value is null or undefined, a radio button <input> with ng-value="false" won't be shown as checked.

When the model value is "" or 0, a radio button <input> with ng-value="false" will be shown as checked.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.
http://plnkr.co/edit/oY0YipxdTJtjjZDDCDDu?p=preview

What is the expected behavior?
I understand the intent here (need for a "blank" value), but if type coercion is going to happen, shouldn't we either go all in, or not do it at all? This "halfway" type coercion is confusing. There should be some sort of option for ng-value that lets you turn on/off strict type coercion.

@Narretz
Copy link
Contributor

Narretz commented Oct 17, 2016

To determine the checkedness, the $viewValue and the ngValue are compared. In 1.5.8 this comparison is not strict. This has been fixed in master and the upcoming 1.5.9.
This makes more sense than the non-strict comparison, because ngValue is an Angular expression.

In 1.5.9, this might actually be a breaking change, but going forward this is the right thing to do,

I understand the intent here (need for a "blank" value)

What do you mean by that?

@jongunter
Copy link
Author

Gotcha! thanks.

As far as a "blank" value goes, say you have two radio buttons, one with ng-value="true" and the other with ng-value="false". When you load the form, you may want both radio buttons to be initially unchecked (thus forcing the user to consider the options and choose a value). ng-value="false" not being checked when the $viewValue is undefined or nullsolves that problem.

Narretz added a commit to Narretz/angular.js that referenced this issue Oct 18, 2016
Closes angular#15283

BREAKING CHANGE:

When using input[radio], the checked status is now determined by doing
a strict comparison between the value of the input and the ngModel.$viewValue.
Previously, this was a non-strict comparison (==).

This means in the following examples the radio is no longer checked:

```
  <!-- this.selected = 0 -->
  <input type="radio" ng-model="$ctrl.selected" value="0" >

  <!-- this.selected = 0; this.value = false; -->
  <input type="radio" ng-model="$ctrl.selected" ng-value="$ctrl.value" >
```

The migration strategy is to convert values that matched with non-strict
conversion so that they will match with strict conversion.
@Narretz
Copy link
Contributor

Narretz commented Oct 18, 2016

That's right.
I have to correct myself. Unfortunately, this is not gonna be part of 1.6, as this is frozen for breaking changes.

@Narretz Narretz modified the milestones: 1.7.0, 1.5.x Oct 18, 2016
Narretz added a commit that referenced this issue Nov 8, 2016
Closes #15283
Closes #15288

BREAKING CHANGE:

When using input[radio], the checked status is now determined by doing
a strict comparison between the value of the input and the ngModel.$viewValue.
Previously, this was a non-strict comparison (==).

This means in the following examples the radio is no longer checked:

```
  <!-- this.selected = 0 -->
  <input type="radio" ng-model="$ctrl.selected" value="0" >

  <!-- this.selected = 0; this.value = false; -->
  <input type="radio" ng-model="$ctrl.selected" ng-value="$ctrl.value" >
```

The migration strategy is to convert values that matched with non-strict
conversion so that they will match with strict conversion.
@gkalpak gkalpak modified the milestones: 1.6.0-rc.1, 1.7.0 Nov 8, 2016
@gkalpak
Copy link
Member

gkalpak commented Nov 8, 2016

This did make it into 1.6.0-rc.1 after all. Yay!

ellimist pushed a commit to ellimist/angular.js that referenced this issue Mar 15, 2017
Closes angular#15283
Closes angular#15288

BREAKING CHANGE:

When using input[radio], the checked status is now determined by doing
a strict comparison between the value of the input and the ngModel.$viewValue.
Previously, this was a non-strict comparison (==).

This means in the following examples the radio is no longer checked:

```
  <!-- this.selected = 0 -->
  <input type="radio" ng-model="$ctrl.selected" value="0" >

  <!-- this.selected = 0; this.value = false; -->
  <input type="radio" ng-model="$ctrl.selected" ng-value="$ctrl.value" >
```

The migration strategy is to convert values that matched with non-strict
conversion so that they will match with strict conversion.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants