-1

I have 2 controllers, loginCtrl and regCtrl, there is a function inside the login controller $scope.login and i want to call this function from inside my regCtrl. What i am trying to achieve is to redirect an user to his homepage without need for login after successful registration. Login function in loginCtrl

 $scope.login=function(){
        NProgress.start();
        singInClicked = true;
        var request =$http({
              withCredentials: true,
              method:"post",
              url: "http://localhost:1337/auth/login",
              data: {
                    user_identifier: $scope.user_identifier,
                    password: $scope.password
              }
        });
and the code of the regCtrl where i want to call this function is 

request.success(
        function(data){
            $scope.response=data;
            console.log("you have registered successfully");
            //serviceStatus($http);
            //**call $scope.login here**
            NProgress.done();
        });

How do i achieve this?

PSL
  • 123,204
  • 21
  • 253
  • 243
Gaurav
  • 609
  • 8
  • 20

1 Answers1

0

One way would be to inject a $controller service in your reg controller, assign a new scope while creating the $controller and use that scope to call the function on loginCtrl.

app.controller('regCtrl', ['$scope', '$controller', function($scope, $controller) {

    var loginCtrlVM = $scope.$new(); //Create a new scope
    //var loginCtrlVM = $scope.$new(true);  //Or even an isolated scope

    $controller('loginCtrl', {$scope:loginCtrlVM }); //Instantiate loginCtrl passing in the scope

     request.success(function(data){
        ...
        console.log("you have registered successfully");

        loginCtrlVM.login(); //<--- Now call login from your own scope

        NProgress.done();
    });

}]);

Another way would be abstract out the logic to a separate service and use it in both the controllers, or even in a common route logic which checks if the user has logged in (Your registration service can perform login action as well by calling the login service to set the logged in status). This way you dont have to have this kind of dependency in your controllers. It also seems like you have NProgress.start(); in one controller and NProgress.done(); in a different controller (probably i have not see the whole code) which makes it look spaghetti.

PSL
  • 123,204
  • 21
  • 253
  • 243
  • Doesn't work gives an error message in the console saying Error: [$injector:modulerr] Failed to instantiate module altonitoApp due to: [$injector:modulerr] Failed to instantiate module altonitoApp.index-controllers due to: [$injector:nomod] Module 'altonitoApp.index-controllers' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. – Gaurav Aug 29 '14 at 03:45
  • That is your app initialization error, you might have some syntax errors or your name might be wrong. Please prepare a plunker. This error message should not appear when you instantiate a controller, unless that controller is not available or is in a differet app which this app does not depend upon. – PSL Aug 29 '14 at 03:47
  • @Gaurav Here is a demo example. Check the console and script.js. http://plnkr.co/edit/0uKVjD?p=preview You have some other issue. You removed "..." i had put in my answer right? – PSL Aug 29 '14 at 03:58