Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in Angular example #31814

Closed
rakeshTRF opened this issue Jul 24, 2019 · 11 comments
Closed

Issue in Angular example #31814

rakeshTRF opened this issue Jul 24, 2019 · 11 comments

Comments

@rakeshTRF
Copy link

rakeshTRF commented Jul 24, 2019

Hi,
Can you please elaborate the [checked]="toggleHeroes" as you can see on below attached image. I think it should be [checked]="showHeroes"

image

馃摎 Docs or angular.io bug report

Description

A clear and concise description of the problem...

馃敩 Minimal Reproduction

What's the affected URL?**

https://angular.io/...

Reproduction Steps**

Expected vs Actual Behavior**

馃摲Screenshot

馃敟 Exception or Error





馃實 Your Environment

Browser info

Anything else relevant?

@kapunahelewong
Copy link
Contributor

kapunahelewong commented Jul 24, 2019

Thank you, @Rakeshkohli. I'll get confirmation, but your question seems relevant.

Adding link for reference. All the other checkboxes use [checked]="show___".

https://stackblitz.com/angular/gdearxmxrem

@kapunahelewong kapunahelewong added this to Need Assistance from Eng. in docs Jul 24, 2019
@dariobraun
Copy link
Contributor

I think @Rakeshkohli is right and it should be [checked]="showHeroes for consistency purposes. I'm actually surprised [checked]="toggleHeroes also works - could someone explain this behavior?

@kapunahelewong
Copy link
Contributor

Agreed, @dariobraun! I couldn't figure out how it was working both ways either. I've raised a flag on this one for engineering help, so we should get clarification soon.

@rakeshTRF
Copy link
Author

rakeshTRF commented Aug 2, 2019

@dariobraun @kapunahelewong
Second, I am getting unchecked checkbox when I remove function - toggleHeroes() from ts file. It means this function giving true value to checkbox that's why it is checked on page load but as we know function is called on click. This is very surprised thing.

@dariobraun
Copy link
Contributor

Any input on this?

@kapunahelewong
Copy link
Contributor

Thanks for the bump, @dariobraun. Let me see if I can get some input. I think the right thing to do is change it. But why @Rakeshkohli gets the unchecked checkbox when removing the function, I'm not sure. I'll be back with some more info.

@gkalpak
Copy link
Member

gkalpak commented Aug 22, 2019

Here is why it (kind of) works with [checked]="toggleHeroes":

What [checked]="<expression>" does is evaluate <expression> and set the .checked property of the input to the resulting value. Of course, the correct expression would be showHeroes, which is a boolean property on the component instance.

By using toggleHeores (which is a method on the component instance), Angular will set input.checked to componentInstance.toggleHeroes. Since methods are functions and functions are truthy in JavaScript, the browser will consequently set input.checked to true. This will make the checkbox checked on load, which also happens to coniside with showHeroes being initialized to true. (I.e. if showHeroes was set to false in the component instance, the checkbox would still be checked - which would not be our intention.)

Now, upon clicking on the checkbox, the default browser behavior will kick in and toggle the checkbox's checked property. At the same time, Angular will evaluate the (click) handler expression (toggleHeroes()) and correctly toggle showHeores, which is turn determines whether the heroes section will be shown or hidden (due to *ngIf="showHeroes").

Finally, Angular will again evaluate the [checked] expression, which will always evaluate to the toggleHeroes method (note it evaluates to the method itself, it does not call the method). Since the value is the always the same as the previous value (the toggleHeroes method), Angular will skip updating the .checked property, leaving it to value currently set by the browser built-in click behavior.

(Note that if Angular didn't have the optimization of skipping updating the property if the expression value hasn't changed since the last check, then Angular would always update input.checked to a truthy value and thus the checkbox would always be checked. So the example only works, because of this optimization, which basically makes [checked]="toggleHeroes" only take effect on page load.)

I hope that (kind of) makes sense. Let me know if anything isn't clear.

And just to be clear, yes, the example is wrong and it should be showHeroes, as already mentioned 馃榿

@rakeshTRF
Copy link
Author

@gkalpak

And just to be clear, yes, the example is wrong and it should be showHeroes, as already mentioned ========> Okay.

@gkalpak
Copy link
Member

gkalpak commented Aug 23, 2019

Reopening the issue, since the issue still needs to be fixed.
For reference, here is the line that needs fixing: https://github.com/angular/angular/blob/c624b14e8/aio/content/examples/http/src/app/app.component.html#L3

@gkalpak gkalpak reopened this Aug 23, 2019
@rakeshTRF
Copy link
Author

@gkalpak

Great :)

kapunahelewong added a commit to kapunahelewong/angular that referenced this issue Aug 23, 2019
atscott pushed a commit that referenced this issue Aug 23, 2019
ngdevelop-tech pushed a commit to ngdevelop-tech/angular that referenced this issue Aug 27, 2019
@jbogarthyde jbogarthyde moved this from Need Assistance from Eng. to Done in docs Aug 29, 2019
sabeersulaiman pushed a commit to sabeersulaiman/angular that referenced this issue Sep 6, 2019
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 23, 2019
arnehoek pushed a commit to arnehoek/angular that referenced this issue Sep 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.