4

I am having two controllers in two different modules,need to call a child controller function in parent controller.Already tried $rootScope but its not working in this case.

Here is code for child controller function:

$scope.processSignOut = function () {
            LogoutService.save(
                function (response) {
                    $state.go('support.login');
                }, function (error) {
                    showAlert('danger',
                        'logout unsuccessfull. Please try again.');
                });
        };

Parent Controller

 $rootScope.logout = function () {
            $rootScope.processSignOut();
        };

Html Code

<button type="button" class="btn btn-secondary btn-block"
   ng-click="logout()">Logout
</button>
user3415447
  • 101
  • 3
  • 13
Amit Anand
  • 95
  • 1
  • 1
  • 6

1 Answers1

1

The solution described here: angularJS: How to call child scope function in parent scope

In your case:

function ParentCntl($scope) {
    $scope.logout = function(){
        $scope.$broadcast ('processSignOut');  
    }
}

function ChildCntl($scope) {               
    $scope.$on('processSignOut', function(e) {  
        $scope.processSignOut();        
    });

    $scope.processSignOut = function () {
        LogoutService.save(
            function (response) {
                $state.go('support.login');
            }, function (error) {
                showAlert('danger',
                    'logout unsuccessfull. Please try again.');
            });
    };
}
Community
  • 1
  • 1
eladc
  • 243
  • 2
  • 6