Call the function of the child controller in the parent controller

I have two controllers in two different modules, I need to call the function of the child controller in the parent controller. Already tried $ rootScope, but it does not work in this case.

Here is the code for the function of the child controller:

$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>
+4
source share
1 answer

The solution described here: angularJS: calling a function of a child in parent space

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.');
            });
    };
}
+1
source

Source: https://habr.com/ru/post/1614997/


All Articles