我在两个不同的模块中有两个 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/