Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My app uses $state.go when switching between tabs and this cause to re-initializes the controllers and scope variable in those controllers get updated and causes memory leaks. Is there a way to stop re-initializing the controllers but change URL on state change?

Example below is routes.js in my app.

.state('home', 
{
    abstract    : true,            
    url         : '/home',
    templateUrl : 'scripts/home.html'
})
.state('home.summary', 
{
    url        : '/summary?userId', 
    controller : 'SummaryCtrl',                
    views      : 
    {               
        summary: 
        {
            templateUrl: 'scripts/home/summary.html'
        }
    }
})
.state('home.summary.detail', 
{
    url        : '/detail/:id',
    controller : 'DetailCtrl',                 
    views      : 
    {               
        detail: 
        {
            templateUrl: 'scripts/home/detail.html'
        }
    }
})

How to stop reloading DetailCtrl but change URL when going to state home.summary.detail if the DetailCtrl is already loaded for unique id???

Also tried to $q.reject in resolve of child state, it stops reload of controllers but doesn't change url.

Also tried reloadOnSearch=false it stops reload of controllers but doesn't change url.

share|improve this question

2 Answers 2

You can use $state.transitionTo instead of $state.go . $state.go calls $state.transitionTo internally but automatically sets options to { location: true, inherit: true, relative: $state.$current, notify: true } . You can call $state.transitionTo and set location: false . For example:

$state.go('.detail', {id: newId}) 

can be replaced by

 $state.transitionTo ('.detail', {id: newId}, { location: false, inherit: true, relative: $state.$current, notify: true }) 
share|improve this answer
    
I think you can just do $state.go('.detail', {id: newId}, {location: false}) –  Resist Design Sep 26 at 20:53
    
I think so, {location: false} is the key option to do that –  rezCash Oct 1 at 16:43

You can have a abstract controller that contain your memory leak code. And a abstract controller will not update when url change.

Here is the doc from official github repo

https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views

look at the Combination part.

here is the plnkr, contacts controller will only call once in this demo contacts.detail controller will update every time you change state but share the scope with contacts controller http://plnkr.co/edit/gmtcE2?p=preview

share|improve this answer
    
i have removed the scope variables but still i wouldn't want the child state controllers to reload if they are already loaded.There should be some way to stop reloading controllers but change url on state transistion?? –  EnugulaS May 1 at 6:40
    
Also tried to $q.reject in resolve of child state, it stops reload of controllers but doesn't change url. Also tried reloadOnSearch=false it stops reload of controllers but doesn't change url. –  EnugulaS May 1 at 7:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.