我目前有2個javascript控制器:selectAll和listAllDomains。 我試圖調用該方法getBeanName(在listAllDomains定義)控制器全選下:在angularjs中調用另一個控制器的函數
response = $http.post('/invoke/selectAll/', INSERT_CALL_HERE);
作爲一個說明,該方法getBeanName將作爲參數傳遞給$ HTTP傳遞返回的對象。 post()方法並由Java控制器處理。
app.controller('listAllDomains', ['$http', '$scope', '$rootScope', function ($http, $scope, $rootScope) {
$scope.showSpinner = true;
$scope.showBtn = false;
let dataObj;
$http.get('domains/').then(function (response) {
$scope.showSpinner = false;
$scope.domains = response.data;
});
$scope.showButton = function(eleStatus) {
let eleSize = Object.keys(eleStatus).length;
return !eleSize;
};
$scope.getBeanName = function (objectType, objectName) {
dataObj = {
type : objectType,
name : objectName,
methodName : "select",
methodParameters : ["true"]
};
localStorage.setItem("dataObj", dataObj);
console.log(dataObj);
$rootScope.$emit("invokeSelectAll", dataObj);
return dataObj;
}
}]);
app.controller('selectAll', ['$http', '$scope' , '$rootScope',
function ($http, $scope, $rootScope) {
var response;
$rootScope.$on("invokeSelectAll", function(){
$scope.invokeSelectAll();
});
$scope.invokeSelectAll = function(){
console.log(localStorage.getItem("dataObj"));
response = $http.post('/invoke/selectAll/', INSERT_CALL_HERE);
response.success(function(data, status, headers, config) {
$scope.responses = data ? data : "Select Operation not supported on this bean";
});
}
}]);
任何幫助,將不勝感激!謝謝!
控制器之間的任何父子關係? – Rahul
定義服務中的getBeanName方法,並將該服務注入到兩個控制器中。然後你可以從你注入服務的任何控制器中調用它。 –
[在AngularJS控制器之間共享數據]的可能的副本(https://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers) – anoop