繁体   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