0

I have this routes:

$routeProvider
            .when('/', {
                templateUrl: 'views/main.html',
                controller: 'MainCtrl',
                controllerAs: 'main'
            })
            .when('/next', {
              templateUrl: 'views/next.html',
              controller: 'MainCtrl',
              controllerAs: 'main'
            })
            .otherwise({
                redirectTo: '/'
            });

When I'm in main page. I clicking to next button and see 'views/next.html'. But I'm not have my previously $scope. How I can get this scope?

  • 2
    the best here is to use service – webduvet Jul 23 '15 at 13:16
  • Maybe you should create a Factory\Service and store all needed data here? And get data through any controller you want. http://stackoverflow.com/questions/26591837/angularjs-store-data-factory – DonSinDRom Jul 23 '15 at 13:17

1 Answers1

6

New route renders new template and also bind new scope to template. So you can't really just use the same scope for both routes. However, there are at least two workarounds.

1). $rootScope. $rootScope is available in all views so you can store some data in it and be able to access it in any template.

2). Service. Use shared service in both controllers. This is probably optimal solution for most cases.

dfsq
  • 191,768
  • 25
  • 236
  • 258