Michael B
Michael B

Reputation: 7587

(newbie) ng-switch in a custom directive breaks two-way binding?

I'm running into an issue with two way binding in angular on a custom directive. I have a directive that will have a editor mode (and have different types of inputs) and display mode.

unfortunately, it seems that if there is a ng-switch the two-way binding breaks from the control. But the variables remain linked if I access it from an external component. Here is a very cutdown example plunker to show the problem.

http://plnkr.co/edit/M8gPfRlrVIXHdXREN1ai

If you modify the top input the changes propogate to the bottom input. But if you modify the bottom input the binding breaks. How can I resolve this issue so that the changes to ng-model in the directive propagate to the controllers scope?

Upvotes: 3

Views: 960

Answers (1)

Ajay Singh Beniwal
Ajay Singh Beniwal

Reputation: 19037

You are facing this problem because Ng-Switch creates its own scope

So there are two solutions to this problem

1) Use two dots in model

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

2) Use $parent in the model

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

Upvotes: 5

Related Questions