Gianfra
Gianfra

Reputation: 1149

How to pass object with ui router between controllers in a $state angularjs

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.

Upvotes: 4

Views: 8327

Answers (4)

Karthikvenkat86
Karthikvenkat86

Reputation: 84

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

Upvotes: 0

DannyGolhar
DannyGolhar

Reputation: 206

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; 

Upvotes: 10

Billy
Billy

Reputation: 1104

Use the resolve statement for ui.router

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

Upvotes: 0

tommyd456
tommyd456

Reputation: 10673

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?

Upvotes: 0

Related Questions