I'm developing a simple shopping cart tracking application to expand my knowledge of ui-router. I have eight different possible states, and each state has its own controller. Here is my app.config file where you can see the state definitions:
module.config(function ($stateProvider, $urlRouterProvider, $locationProvider) { $stateProvider .state("empty", { templateUrl: "app/templates/empty.html", controller: 'emptyController', controllerAs:'vm' }) .state("initial", { templateUrl: "app/templates/initial.html", controller: 'initialController', controllerAs: 'vm' }) .state("shopping", { templateUrl: "app/templates/shopping.html", controller: 'shoppingController', controllerAs: 'vm' }) .state("shipping", { templateUrl: "app/templates/shipping.html", controller: 'shippingController', controllerAs: 'vm' }) .state("billing", { templateUrl: "app/templates/billing.html", controller: 'billingController', controllerAs: 'vm' }) .state("review", { templateUrl: "app/templates/review.html", controller: 'reviewController', controllerAs: 'vm' }) .state("confirmation", { templateUrl: "app/templates/confirmation.html", controller: 'confirmationController', controllerAs: 'vm' }) .state("error", { templateUrl: "app/templates/error.html", controller: 'errorController', controllerAs: 'vm' }) $locationProvider.html5Mode(true); $urlRouterProvider.otherwise("/"); }) I'm trying to track the 'cart' using an object called cart, defined in a factory:
function cart() { var factory = {}; factory.model = {}; return factory; } On my navbar, I have ui-sref's to change the state:
<li ui-sref-active="active"><a ui-sref="empty">Empty</a></li> <li ui-sref-active="active"><a ui-sref="initial">Initial</a></li> <li ui-sref-active="active"><a ui-sref="shipping">Shipping</a></li> <li ui-sref-active="active"><a ui-sref="billing">Billing</a></li> <li ui-sref-active="active"><a ui-sref="review">Review</a></li> <li ui-sref-active="active"><a ui-sref="confirmation">Confirmation</a></li> <li ui-sref-active="active"><a ui-sref="error">Error</a></li> <li ui-sref-active="active"><a data-toggle="modal" data-target="#login-modal">Log in</a></li> Whenever I change state, I need to transfer the entire cart.model to the new state. How can I do this? My controllers each look like this:
(function () { "use strict"; angular.module('ShoppingCart') .controller('emptyController', ['cart', emptyController]); function emptyController(cart) { var vm = this; vm.model = cart.model; }; }()); I'm trying to get the cart's model, but I'm not sure how I can update it on state change.