1

I'm new to AngularJS, I want to pass dynamic value (username) from one controller in one module to another controller in another module. Routing and other things are working fine. This is my code

loginModule.js

(function() {

      var app = angular.module("waleteros", ["ngRoute","ui.bootstrap","ngCookies"]);

        app.config(function($routeProvider) {
            $routeProvider
            .when("/",{
                templateUrl:"views/login.html",
                controller:"LoginCtrl"
            })
        }
    })

app.js

(function() {

      var app = angular.module("waleterosAdmin", ["ngRoute","ngGrid","ui.bootstrap","ngCookies"]);

      app.config(function($routeProvider) {
        $routeProvider
            .when("/home",{
                templateUrl:"homepage.html",
                controller:"HomeCtrl"
            })
        }
    })

loginCtrl.js

(function(){

        var app = angular.module("waleteros");

        var LoginCtrl= function($scope,$location)
        {
            $scope.signIn=function(email,pin)
            {
               //Some authentication code...
               //Here i want to pass the username to homectrl.js
               window.location.href="views/home.html"
            }
        }
        app.controller("LoginCtrl", LoginCtrl);
    })

HomeCtrl.js

 (function(){

        var app = angular.module("waleterosAdmin");

        var HomeCtrl=function($scope)
        {
             //Here i want to get the username
        }

        app.controller("HomeCtrl", HomeCtrl);
    })
srihari
  • 397
  • 2
  • 7
  • 18

3 Answers3

0

you can share service between modules ,and thus pass value between modules, please have a look here Share a single service between multiple angular.js apps ,and here sharing between modules with AngularJS?

Community
  • 1
  • 1
Kostia Mololkin
  • 868
  • 9
  • 25
0

You would use a service to persist the data, and then inject the service into your controllers:

app.service("SharedProperties", function () {
    var _userName = null;

    return {
        getUser: function () {
            return _userName
        },

        setUser: function(user) {
            _userName = user;
        }
    }
});

Now inject SharedProperties and use the getter/setter

var LoginCtrl= function($scope,$location, SharedProperties)
    {
        $scope.signIn=function(email,pin)
        {
           //Some authentication code...

           SharedProperties.setUser(user);
           //Here i want to pass the username to homectrl.js
           window.location.href="views/home.html"
        }
    }

var app = angular.module("waleterosAdmin");

    var HomeCtrl=function($scope, SharedProperties)
    {
         //Here i want to get the username
         var user = SharedProperties.getUser();
    }
tymeJV
  • 103,943
  • 14
  • 161
  • 157
0

One word of warning about services is that they persist for the lifetime of the application, i.e. they are only instantiated once. I have run into scenarios, especially once routing is implemented, where I want to wipe the data off of the service to save space and replace it with new data (you don't want to keep adding to this service every time you look at a different view). To do this, you could either write a "wipe" method that you call to clean the service on the route changes, or stick the data into a directive (on its controller), put the controllers into their own directives, and have these require the first directive, so that the data is accesible from the controller's with the added benefit of being wiped once the DOM element is declared on is destroy (on a view change, for instance).

Derek
  • 722
  • 1
  • 6
  • 16