我在两个不同的模块中有两个控制器,需要在父控制器中调用子控制器功能.已经尝试过$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
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.'); }); }; }