angularjs - 在父 Controller 中调用子 Controller 功能

标签 angularjs

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

这是子 Controller 功能的代码:

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

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

关于angularjs - 在父 Controller 中调用子 Controller 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33602946/

相关文章:

javascript - Angular-为什么/in href 停止闪烁?

angularjs - 是否有更紧凑的方法在函数内使用 AngularJS 返回延迟?

javascript - 选择的 Angular 指令不会更新

angularjs - 根据 Angular 的路线变化添加或删除类

javascript - 如何显示 XHR 请求的加载图标?

javascript - 无法将数据从 Controller 绑定(bind)到 View 。控制台没有错误

javascript - 我的服务没有更新 - Angularjs

javascript - 括号围绕作业完成什么任务

javascript - 如何访问 angularjs promise 中的数据?

javascript - Javascript 中返回值,但结果是未定义的