Share data between AngularJS states

100 Views Asked by At

So I've 2 states configured to use the SAME controller, as follows:

$stateProvider
    .state('productsList', {
            url: '/products',
            templateUrl: 'views/productList.html',
            controller: 'ProductsCtrl'  //same controller
        })
    .state('productDetail', {
            url: '/products/:productId',
            templateUrl: 'views/productDetail.html',
            controller: 'ProductsCtrl'  //same controller
        })

The first state ("productsList") has some filters fields, and a table (ngTable) loaded with the products list from server according to the filters. The ngTable component is configured to have pagination.

Let's say user is paginating through the table and when he's at page 5, he chooses to edit a Product. State changes to "productDetail". When he finishes editing, he wants to go back to Products lists state exactly where he was (PAGE 5, and with the filters he applied).

But although both states are using the same Controller, when state changes the controller is recreated. And when I'm in the second state I'm not able to get the data from previous state where I had the current page of table, filters etc.

I'm missing something? Or perhaps structuring the app the wrong way? (most likely)

How do you guys would structure for this scenario?

Thanks!

0

There are 0 best solutions below