Search code examples
angularjsangular-ui

Preserve state with Angular UI-Router


I have an app with a ng-view that sends emails to contact selected from a contact list. When the users select "Recipient" it shows another view/page where he can search, filter, etc. "Send email" and "Contact list" are different html partials that are loaded in the ng-view.

I need to keep the send form state so when the users select someone from the Contact List it returns to the same point (and same state). I read about different solutions ($rootScope, hidden divs using ng-show, ...) but I want to know if UI-router will help me with it's State Manager. If not, are there other ready-to-use solutions?

Thanks!


Solution

  • The solution i have gone with is using services as my data/model storage. they persist across controller changes.

    example

    the user service ( our model that persists across controller changes )

    app.factory('userModel', [function () {
        return {
            model: {
                name: '',
                email: ''
            }
        };
    }]);
    

    using it in a controller

    function userCtrl($scope, userModel) {
        $scope.user = userModel;
    }
    

    the other advantage of this is that you can reuse your model in other controllers just as easly.