4

I am using this generator.

I have my user module:

require('angular/angular');
require('angular-ui-router');


angular.module('userModule', ['ui.router'])
    .config(['$stateProvider', function($stateProvider) {


    $stateProvider
        .state('utenti', {
            url: '/utenti',
            templateUrl: 'src/utenti/views/utenti.tpl.html',
            controller: 'RegistrationCrl',    
        })

        .state('access', {
            url: '/access',
            templateUrl: 'src/utenti/views/access.tpl.html',
            controller: 'AccessCtrl',
        });


}])
.controller('RegistrationCrl', require('./controllers/registrationLogin'))
.controller('AccessCtrl', require('./controllers/access'));

In my RegistrationCrl I use a service to create a new member:

module.exports = function($scope, User, Member, $state, $location,$rootScope, $stateParams){
        $scope.registration = function(form){
            if(!form.$valid ){
                console.log('input error');
                return;
            }
            else{
                console.log('valid form');

                Member.create($scope.newMember,
                    function(data){
                        $state.go('access', data); 
                    },
                    function(data){
                        console.log(data);
                    }
                );

            }
        };
    };

I would like to use that data in my next view related to the accessCtl. I don't want to use the id in my URL and using the $stateParams.

My access controller is:

module.exports =  function($scope, User, Member, $state, $rootScope, $stateParams){
        console.log($state);
        console.log($stateParams);
    };

In that console.log I don't find my object passed with $state.go.

3
  • Can't you pass your data in $state as you pass the controller? .state('access', { url: '/access', templateUrl: 'src/utenti/views/access.tpl.html', controller: 'AccessCtrl', newData: 'whatever' }); It works with routes. Commented Jan 22, 2015 at 14:25
  • I have read about that but what i miss is how to put my data result from the registration in the newData property, what should i write in my registrationCrl? $state.go('access', {newData:data} ? Commented Jan 22, 2015 at 14:37
  • Oh, I've misunderstood you. I thought you were trying to pass a fixed value to your controller. So, I don't know. Sorry. Commented Jan 22, 2015 at 14:48

4 Answers 4

10

You can $stateParams to pass data to next view. Add param in target route

 $stateProvider
    .state('utenti', {
        url: '/utenti',
        templateUrl: 'src/utenti/views/utenti.tpl.html', 
        params: { myParam: null},
        controller: 'RegistrationCrl',    
    })

Pass data to next view

  $state.go("utenti",{myParam:{"data":"data"}})

And get data in next controller

         $scope.paramDetails=$stateParams.myParam; 
Sign up to request clarification or add additional context in comments.

Comments

0

It's my understanding that you can't pass objects between states as params. One option is to use a service which is accessible by all controllers that you inject it into.

There is a good answer already on SO for sharing a service Passing data between controllers in Angular JS?

2 Comments

I have read that answer but using a controller can be a bit messy. If i need to use this data in more views i have to add a function or a nee service every time. If i can't pass objects, can i pass some attributes of the objects?
Pass some reference to the data and then search for it on your backend perhaps.
0

Use the resolve statement for ui.router

 resolve:{
      data: ['$stateParams', function($stateParams){
          return $stateParams.data;
      }]

1 Comment

I use the resolve statement in my access state. But in my other controller (RegistrationCtr) how do i assign that data to my data? :) $state.go('access', data) ?
0

You can set though the state param.

$state.get('details').Obj= {};
$state.go('details);

the access like below in the another controller which is attached to this state.

$state.get('details').Obj

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.