I have a ui-router application with a lot of states, and I need to be able to pass the model through $ stateParams. For every $ http request, I check the response for the "STATE" parameter, which is returned from the server. If it exists, I execute $state.go(STATE) .
So efficiently, I have $stateProvider :
$stateProvider .state('Account', {url: '/Account', template: '<ui-view/>'}) .state('Account.name', { url: '/Name', templateUrl: 'app/Account/partials/Name.html', controller: 'AccountNameController as nameVm' })
And many more conditions that look like this.
I have a data model, which is just a factory with an object that receives and sets through functions. Therefore, whenever I call saveAccount (), it takes a model and sends it to the Web API backend. The backend validates the data and sends it using the STATE parameter (either account.invalid , account.valid , or account.needsMoreInfo ). Here is my $ httpInterceptor
.factory('httpInterceptor', ['$q', '$injector', function ($q,$injector) { return { 'response': function(response) { if(response.data.state){ $injector.get('$state').go(response.data.state, response.data.account); } return response; } }; } ])
As you can see, I am trying to send an account through state templates.
In the controller, I basically need to say vm.account = $stateParams.account
My question is:
How can I change my $ states to have a named controller, and also take a state parameter and access this from the controller?
The reason I don't pass data through the service is because there are several models, so I cannot just specify the name of the service in $ httpInterceptor, because it is not constant.
EDIT: Calculated
My controller should have been here: if ($ stateParams & $ stateParams.data) {vm.Account = $ stateParams.data; }
And here is what state in the end looked like this:
.state('taxAccount.invalid', { url: '/Invalid?params', templateUrl: 'app/TaxAccount/partials/Invalid.html', controller: 'taxAccountInvalidController as invalidVm', params:{data:null} })
I did not understand that I could put params:{data:null} . I thought stateParams should have entered the controller declaration in the state configuration.