Scope changes after ng-switch

744 views Asked by At

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.

1

There are 1 answers

0
Michael On

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.