Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 313
Gianfra Avatar asked Dec 24 '22 23:12

Gianfra


1 Answers

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; 
like image 59
DannyGolhar Avatar answered Apr 19 '23 20:04

DannyGolhar