1

I have a switch that changes the value of a variable called p.

<div ng-switch on="p">
   <div ng-switch-when="true">
       /*...show nodes and ability to add node*/
   </div>
   <div ng-switch-when="false">
       /*for now show nothing*/
   </div>
</div>

In my controller:

$scope.nodes=[{node1},{node2},{node3}];

function to add a node

$scope.$watch('nodes', function(nodes) {
    console.log(nodes); 
    console.log("================");   
},true);

PROBLEM: when i print $scope nodes in the above all the new nodes are shown. If I switch OFF and ON (p=false and then p=true) I have the initial $scope.nodes. Why on earth are my nodes reset on switch? See this example: plunker

EDIT: ng-switch worked with no scope change if I didn't have a directive but reinitialized my scope when I used a directive inside it. Although I haven't understood exactly why I dropped ng-switch and used ng-show instead.

Community
  • 1
  • 1
Anna
  • 101
  • 8
  • Can't understand, you are switching on `$scope.p` but you are watching `$scope.nodes`. When do you change `$scope.p`? – michelem Jun 19 '15 at 09:06
  • can you paste the code you are using to add nodes in html? and the function to add nodes – akashrajkn Jun 19 '15 at 09:08
  • I have an on off switch like this one: https://proto.io/freebies/onoff/ The switch is done, I change views. The problem is why do my nodes change? @akashrajkn the nodes change through a directive, that is why I skipped the function. Since my console.log shows that $scope.nodes are updated correctly why would a simple change of view reset them? – Anna Jun 19 '15 at 09:09
  • can you make a plunker or fiddle? mostly the problem is in the function to add node, but difficult to tell until you give more details – akashrajkn Jun 19 '15 at 09:11
  • 1
    If the node is defined within the scope of the current view, switching view re-initializes a controller and thus the scope. That might be why the nodes are reset – Guinn Jun 19 '15 at 09:14
  • @Guinn yes the node is defined withing the scope of the current view, but both views share the same controller. – Anna Jun 19 '15 at 09:21
  • It should not. Something else is going on. Can you post a fiddle? – Michael Kang Jun 19 '15 at 09:26
  • If you, in the new view, use something like
    , that very statement tells the app to re-initialize (or create a second scope) of that controller.
    – Guinn Jun 19 '15 at 09:28
  • Guys my code is huge... I ll try to keep what's important to make a fiddle but till then can you think of any reasons this my happen? Thank you for all your answers – Anna Jun 19 '15 at 09:29
  • Sorry but that's not sufficient. You should try to post a smaller example of your code – michelem Jun 19 '15 at 09:30

1 Answers1

0

It would help to see the controller and on which level it is initialized. The ngSwitch will not just hide the content - it will remove and add the html and initialize the controllers and directives each time a switch is made. Probably the nodes get initialized there as well.

Michael
  • 3,085
  • 1
  • 17
  • 15