0

I currently have 2 javascript controllers: selectAll and listAllDomains. I'm trying to call the method getBeanName (defined in listAllDomains) in the controller selectAll under:

response = $http.post('/invoke/selectAll/', INSERT_CALL_HERE);

As a note, the object returned by the method getBeanName will be passed as an argument to the $http.post() method and handled by a Java controller.

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


}]);

Any help would be much appreciated! Thank you!

George Cimpoies
  • 884
  • 2
  • 14
  • 26
  • any parent child relationship between controllers ? – user1608841 Aug 29 '17 at 10:05
  • 3
    Define the `getBeanName` method in a service, and inject that service into both of your controllers. Then you can call it from any controller in which you have injected the service. – bamtheboozle Aug 29 '17 at 10:05
  • 2
    Possible duplicate of [Share data between AngularJS controllers](https://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers) – anoop Aug 29 '17 at 10:12
  • Solution was to have &rootScope.dataObj instead of INSERT_CALL_HERE – George Cimpoies Aug 29 '17 at 11:23

2 Answers2

0

first create a service/factory with required function. And inject into the controller.

0

You can either create a service and inject it in both controllers or just define your function in $rootScope and then use it in any controller.

Check this issue for reference: How to use $rootScope in AngularJS

anteAdamovic
  • 1,462
  • 12
  • 23