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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some inputs inside ng-switch don't update the model when changed #1139

Closed
racheldale opened this Issue Jul 12, 2012 · 8 comments

Comments

Projects
None yet
5 participants
@racheldale

racheldale commented Jul 12, 2012

Inputs that point to fields directly on the scope (e.g. data-ng-model="fieldName") don't update when inside of an ng-switch case. This works fine for fields that are inside an object (e.g. data-ng-model="item.fieldName").

Here is an example: http://jsfiddle.net/r4RKW/1/

@pkozlowski-opensource

This comment has been minimized.

Show comment
Hide comment
@pkozlowski-opensource

pkozlowski-opensource Nov 12, 2012

Member

@racheldale This works as intended. The ng-switch directive creates a new scope and the input binds to the fieldName in this new scope, instead of the parent one. Scopes prototypically inherit from each other, AngularJS just behavior is consistent with behavior of regular JavaScript objects. More info here: http://docs.angularjs.org/guide/scope

Member

pkozlowski-opensource commented Nov 12, 2012

@racheldale This works as intended. The ng-switch directive creates a new scope and the input binds to the fieldName in this new scope, instead of the parent one. Scopes prototypically inherit from each other, AngularJS just behavior is consistent with behavior of regular JavaScript objects. More info here: http://docs.angularjs.org/guide/scope

@petebacondarwin

This comment has been minimized.

Show comment
Hide comment
@petebacondarwin

petebacondarwin Nov 12, 2012

Member

Yes you are right. ng-switch creates a new scope and so ng-model is writing to an inner scope value and the outer scope value does not get updated. As you say, use objects to refer to the field.

Member

petebacondarwin commented Nov 12, 2012

Yes you are right. ng-switch creates a new scope and so ng-model is writing to an inner scope value and the outer scope value does not get updated. As you say, use objects to refer to the field.

@MrFusion42

This comment has been minimized.

Show comment
Hide comment
@MrFusion42

MrFusion42 Feb 13, 2013

Here is the modified working fiddle for the pragmatic people out there: http://jsfiddle.net/MrFusion/r4RKW/36/

MrFusion42 commented Feb 13, 2013

Here is the modified working fiddle for the pragmatic people out there: http://jsfiddle.net/MrFusion/r4RKW/36/

@jodinathan

This comment has been minimized.

Show comment
Hide comment
@jodinathan

jodinathan Jan 4, 2016

Hi,

I am having this problem, however, I need to bind the ngModel from the parent.
The ng-model attribute of the directive is like "data.title", but the directive creates an element that needs to use the same model.
Inside the template of the directive exists a ng-switch that is troubling me.

How can I bind the model and still uses ng-switch in this case?

jodinathan commented Jan 4, 2016

Hi,

I am having this problem, however, I need to bind the ngModel from the parent.
The ng-model attribute of the directive is like "data.title", but the directive creates an element that needs to use the same model.
Inside the template of the directive exists a ng-switch that is troubling me.

How can I bind the model and still uses ng-switch in this case?

@petebacondarwin

This comment has been minimized.

Show comment
Hide comment
@petebacondarwin

petebacondarwin Jan 4, 2016

Member

@jodinathan - if you provide a plunker (or similar) of your situation you are more likely to get a solution. It is difficult to help with just a description.

Member

petebacondarwin commented Jan 4, 2016

@jodinathan - if you provide a plunker (or similar) of your situation you are more likely to get a solution. It is difficult to help with just a description.

@jodinathan

This comment has been minimized.

Show comment
Hide comment

jodinathan commented Jan 4, 2016

@petebacondarwin

This comment has been minimized.

Show comment
Hide comment
@petebacondarwin

petebacondarwin Jan 4, 2016

Member

You should not be passing ngModel around like that.
Here is better way: http://jsbin.com/yihuyucamu/1/edit?html,js,output

Member

petebacondarwin commented Jan 4, 2016

You should not be passing ngModel around like that.
Here is better way: http://jsbin.com/yihuyucamu/1/edit?html,js,output

@jodinathan

This comment has been minimized.

Show comment
Hide comment
@jodinathan

jodinathan Jan 4, 2016

Hi @petebacondarwin ,

I fixed your JSBIN: http://jsbin.com/behome/edit?html,js,output for anyone that comes here.
Thanks

jodinathan commented Jan 4, 2016

Hi @petebacondarwin ,

I fixed your JSBIN: http://jsbin.com/behome/edit?html,js,output for anyone that comes here.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment