在父控制器中调用子控制器功能

时间:2021-11-06 12:55:26

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.

我在两个不同的模块中有两个控制器,需要在父控制器中调用子控制器功能。已经尝试过$ rootScope但在这种情况下它不起作用。

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

Html代码

<button type="button" class="btn btn-secondary btn-block"
   ng-click="logout()">Logout
</button>

1 个解决方案

#1


1  

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

这里描述的解决方案:angularJS:如何在父范围内调用子范围函数

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


1  

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

这里描述的解决方案:angularJS:如何在父范围内调用子范围函数

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