0

I am building a multi step signup form, in that I am passing the data from 1st state to 2nd state using input boxes, but data is not fetching from the first state

var publisherdata = { email: $scope.publishersignup.email, name: $scope.publishersignup.name, password: $scope.publishersignup.password }; $state.go('profile', publisherdata); 

in html view I am using this publisherdata variable to fetch the details like:

<input type="text" ng-model="email" ng-bind="publisherdata.email" name="email" disabled /> 

But value is nothing. Please help, how to pass data?

1
  • do you have more code to show? perhaps a plunkr? Commented May 24, 2016 at 5:32

2 Answers 2

1

For the bindings to work, you need to define your data as state parameters, inject $stateParams in your destination controller and add the parameters into $scope.

For example,

State configuration:

$stateProvider.state('profile', { url: '/profile', params: { publisherdata: null } templateUrl: '', controller: 'destinationCtrl' }); 

Passing data:

$state.go('profile', { publisherdata: { email: $scope.publishersignup.email, name: $scope.publishersignup.name, password: $scope.publishersignup.password } }); 

Destination controller:

.controller('destinationCtrl', function($scope,$stateParams){ $scope.publisherdata = $stateParams.publisherdata; }); 
Sign up to request clarification or add additional context in comments.

4 Comments

nice example. does this send parameters to the new state? Is this instead of using services ?
@StianStandahl it sends data to new state. $stateParams is a built in ui router service we can use access parameters send to current state
interesting. I wouldn't do it like that, but interesting nevertheless.
Thanks TJ, That's awesome
1

Seems that ng-model="email" points to $scope.email. If you want to fetch this data you can use that variable, or you can use publisherdata.email. I don't think you need ng-bind, ng-model should suffice.

this code $state.go('profile', publisherdata); changes the view from one to another. The variables in the controller will not be used. I recommend making a service or a factory that you can use for sending information from one controller to another.

13 Comments

Using a service is the way to go. State parameters are not there to "pass data".
@TJ I don't see where the disagreeing part is. Passing a data object via state params is possible, will work, but simply not good practice. That will cause you states to be tightly coupled to each other, instead on depending of a single point of dependency that can be injected to them.
@StianStandahl using state params is useful to pass view related information. To share data between state/controllers, delegate the data handing to a service that will be responsible for that data
@TJ it's just a matter of practices. I believe that separating the data to a different service will help in the future, and that makes it a good practice. Lets say that you will need another object to be passed, instead of changing all the states, you will have to change only the service. By the way, a 71 up votes does'nt make it right, there are a lot of questions in SO that the accepted answer is not the best one.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.