ng-switch-when: Does it recreate new controller each time and remove previous data when selecting different step?

StackOverflow https://stackoverflow.com/questions/19183404

I have a single page webapp based on angular.js And seems like I have performance problems. so I have the main controller and view for it which looks like this

<div>
 <div data-ng-switch-when="step1">
  <div data-ng-include="'/partials/step1.html'"></div>
 </div>

 <div data-ng-switch-when="step2">
  <div data-ng-include="'/partials/step2.html'"></div>
 </div>
</div>

for each step I have it's own controller, in our case is step1-ctl and step2-ctl.

So my question is: when I select step2, does angular unbind all elements from step1, remove watches and clean resource? And second question - when I switching between steps, does angular create new controller instance each time, add callbacks, binding, etc?

有帮助吗?

解决方案

Not sure what you mean exactly by "does angular unbind all elements from step1" but what AngularJS is going to do is to destroy a scope created by the ngSwitch directive (plus its children, thus destroying any watches created in the step1) and remove corresponding DOM elements. If you don't use any badly-written directives that could leak resources in the step1.html AngularJS should clean up DOM elements and the corresponding watches.

The answer to your second question is YES.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top