1

I have noticed that, whenever a $state.go() is applied to a state with parameter(s) in AngularJS UI-Router, a new instance of the HTML content [for that view] is created each time. Not only the HTML content, but also the controller are being created as many times as newer params are appearing on the $stateParams after calling $state.go() in the program later on.

How do I stop new instances of HTML content and controller to stop appearing, while still passing the params to the $stateParams? I want to use the same controller, same scope and same HTML content for any data that $stateParams holds. Thank you very much in advance!

9
  • how do u come to know that multiple instance are created ? can you create a fiddle ? Commented Jul 19, 2015 at 7:05
  • I'm not exactly sure what you mean by "instance of the HTML content", or what your exact scenario is, but I think what you see is completely expected. Controllers are not singletons like services are. Each time a view with a controller is displayed, a new scope instance and a new controller is created. Why is that a problem? Commented Jul 19, 2015 at 7:31
  • @ngLover, multiple instances of HTML codes are being created. Suppose I have a code like <div ui-view>content</div>, multiple instances go like <div ui-view>content</div><div ui-view>content</div>. This applies to anything that the UI-router creates. So you can use any fiddle you like, with a state containing a parameter in its url. Commented Jul 19, 2015 at 7:53
  • @JBNizet how do I make controllers singletons? Commented Jul 19, 2015 at 7:54
  • 1
    @DamodarDahal, you need to show some code. It's pointless to speculate otherwise Commented Jul 19, 2015 at 8:36

1 Answer 1

1

YES, it is possible. Disable caching.

This is done in various ways. One of the ways would be to be disable caching while defining the states. Eg:

$stateProvider.state("home",
    {
        url:'/home',
        cache:false,
    }

Adapted from ui.router not reloading controller

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.