[英]How to call function in parent controller from the child controller in angularjs
[英]Call child controller function in Parent Controller
我在兩個不同的模塊中有兩個控制器,需要在父控制器中調用子控制器功能。已經嘗試過$ 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:如何在父作用域中調用子作用域函數
在你的情況下:
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.');
});
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.