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.

demongolem
  • 9,474
  • 36
  • 90
  • 105
Gianfra
  • 1,119
  • 4
  • 17
  • 33
  • 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. – klauskpm Jan 22 '15 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} ? – Gianfra Jan 22 '15 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. – klauskpm Jan 22 '15 at 14:48

4 Answers4

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; 
DannyGolhar
  • 196
  • 1
  • 10
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?

Community
  • 1
  • 1
tommyd456
  • 10,443
  • 26
  • 89
  • 163
  • 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? – Gianfra Jan 22 '15 at 15:10
  • Pass some reference to the data and then search for it on your backend perhaps. – tommyd456 Jan 22 '15 at 15:17
0

Use the resolve statement for ui.router

 resolve:{
      data: ['$stateParams', function($stateParams){
          return $stateParams.data;
      }]
Billy
  • 1,104
  • 8
  • 22
  • 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) ? – Gianfra Jan 22 '15 at 17:21
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