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

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

这是子控制器功能的代码:

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

家长控制器

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

Html代码

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

解决方法

这里描述的解决方案:
angularJS: How to call child scope function in parent scope

在你的情况下:

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

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...