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

Bug in checkbox #10662

Closed
diduweiwu opened this issue Jan 7, 2015 · 23 comments
Closed

Bug in checkbox #10662

diduweiwu opened this issue Jan 7, 2015 · 23 comments

Comments

@diduweiwu
Copy link

I use checkbox like this:

[label][input type="checkbox" ng-checked="go" ng-true-value="true" ng-false-value="false" ng-model="go"][/input]Send out email notification[/label]

the default value of parameter "go" is "true", when I un-checked the checkbox,the value of go is still "true" when I use console.log(go) to trace it. And when I check and then un-check it angain, its value changed.

@caitp
Copy link
Contributor

caitp commented Jan 7, 2015

Hey, thanks for the bug report =) could you please post a minimal reproduction of your issue to http://plnkr.co or http://jsbin.com? thanks~

@diduweiwu
Copy link
Author

Hi @caitp , here I create an example for that
http://plnkr.co/edit/sXFd6hZ8bXRLN1SG3217

@LeonardoBraga
Copy link
Contributor

Initialize go correctly and it will work: http://plnkr.co/edit/49at2ol4W4la4LVrRqcT?p=preview

@LeonardoBraga
Copy link
Contributor

... and you can also remove the ng-false-value and ng-true-value in this case, since they're false and true, respectively, by default. I hope it helps!

@diduweiwu
Copy link
Author

@LeonardoBraga Got it. So its value should be boolean, not string. Hope it would be more convenient because all of my data are get as string.
My problem is solved. Thanks.

@caitp
Copy link
Contributor

caitp commented Jan 7, 2015

he wants the true and false to be string values --- ng-true-value and ng-false-value need to be initialized with constant values. If they are wrapped in quotes like ng-true-value="'true'" it works just fine

@caitp
Copy link
Contributor

caitp commented Jan 7, 2015

@LeonardoBraga
Copy link
Contributor

Sure thing, @caitp. He didn't mention the values had to be strings in the beginning, this is why I considered it being the wrong usage, and suggested using true instead of 'true' and removing ngFalseValue and ngTrueValue directives, which also made the code simpler.

@diduweiwu
Copy link
Author

@caitp Do you mean initialize it like this? But it didn't run properly.
http://plnkr.co/edit/sXFd6hZ8bXRLN1SG3217?p=preview

  1. [input ng-model="go" type="checkbox"
    ng-true-value="'true'" ng-false-value="'false'"
    ng-checked="go">!

    1. $scope.go = 'true';

@diduweiwu
Copy link
Author

@LeonardoBraga Yes, I must use Boolean(go) to convert it to boolean formats. Well, it works fine for me now. Great thanks for your kind help.

@caitp
Copy link
Contributor

caitp commented Jan 7, 2015

@gaoyuan121 hmmm, you're right. That's weird. lets keep this open so that we can fix that bug.

@LeonardoBraga
Copy link
Contributor

@gaoyuan121 The glitch with string values is being caused because the input has both ng-model and ng-checked in it. Remove ng-checked and it will work with the strings as well.

@caitp
Copy link
Contributor

caitp commented Jan 7, 2015

when on earth did we add that? but yeah I don't think that's an excuse --- we probably shouldn't allow ngChecked to operate if it's used on its own input, to prevent weirdness

@LeonardoBraga
Copy link
Contributor

@caitp I can provide a PR for that, if you think it's appropriate.

@caitp
Copy link
Contributor

caitp commented Jan 7, 2015

no let me do it i haven't solved any problems in this repo for at least a month now =) nah i'm kidding, go for it

@LeonardoBraga
Copy link
Contributor

@caitp lol :-)

@diduweiwu
Copy link
Author

@LeonardoBraga Sounds good:)

@petebacondarwin
Copy link
Contributor

I don't understand why we need this fix. What is the need for using ngModel and ngChecked on the same element. They are for different purposes. If you remove the ngChecked everything works fine.

http://plnkr.co/edit/PU8QKeMgITqUbDAm9XlK?p=preview

@petebacondarwin
Copy link
Contributor

@caitp and @gaoyuan121 - can you provide the real world use case ?

@Narretz Narretz added this to the Ice Box milestone Jan 21, 2015
@Narretz
Copy link
Contributor

Narretz commented Jan 21, 2015

It actually doesn't make sense to use ng-checked and ng-model together. I'd be fine with reverting this and updating the docs for ng-checked instead.

@petebacondarwin
Copy link
Contributor

@caitp - can you give a view on reverting this?

@caitp
Copy link
Contributor

caitp commented Jan 21, 2015

I'm anti-reverting, but I'm pro-shrinking if you can find a way to express it in fewer characters.

The API should not be surprising people

@lgalfaso
Copy link
Contributor

With #10664 landed, it should be safe to close this. If anybody wants to talk about the interactions of ng-model and ng-checked, then please open another issue just for this

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.

6 participants