簡體   English   中英

從angularjs中的另一個控制器調用函數

[英]Call function from one controller in another in angularjs

我目前有2個javascript控制器:selectAll和listAllDomains。 我正在嘗試在控制器的selectAll下調用方法getBeanName(在listAllDomains中定義):

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"; }); } }]); 

任何幫助將非常感激! 謝謝!

首先創建具有所需功能的服務/工廠。 並注入控制器。

您可以創建服務並將其注入兩個控制器中,也可以只在$ rootScope中定義函數,然后在任何控制器中使用它。

請檢查此問題以供參考: 如何在AngularJS中使用$ rootScope

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM