0

How to i call function getinfo() in success of employeecontroller function which is used in crudApp.controller so that i can get updated table as soon as i click submit button

var crudApp = angular.module('crudApp', []);
crudApp.controller("DbController", ['$scope', '$http', function ($scope, $http) {

    getInfo();
    function getInfo() {
        $http.post('select.php').success(function (data) {
            // Stored the returned data into scope
            $scope.details = data;
        });
    }

    $scope.deleteInfo = function (info) {
        $http.post('delete.php', {
            del_id: info
        }).success(function (result) {
            getInfo()
            console.log(result);
        });
    }
}]);

function employeecontroller($scope, $http) {
    $scope.insertData = function () {
        $http.post("insert.php", {
            'firstname': $scope.firstname,
            'lastname': $scope.lastname,
        }).success(function (data, status, headers, config) {
            console.log("Data Inserted Successfully");
        });
    }
}
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • This question doesn't really make sense; There seem to be some things wrong with this code; specifically, it's not really clear how you are ever using `employeecontroller`, since it's not an angular controller and you aren't showing the HTML of it's use. – Claies Aug 10 '16 at 12:12

3 Answers3

0

in success of employeecontroller function call getInfo using .injector()

REF:AngularJS. How to call controller function from outside of controller component

 function employeecontroller($scope, $http) {
                                    $scope.insertData = function () {
                                        $http.post("insert.php",{
                                            'firstname': $scope.firstname,
                                            'lastname': $scope.lastname,
                                        }).success(function (data, status, headers, config) { 
                                            console.log("Data Inserted Successfully");
                                           angular.element(document.getElementById('yourControllerElementID')).injector().‌​getInfo('$rootScope');
                                        });
                                    }
                                }
Community
  • 1
  • 1
shan kulkarni
  • 849
  • 7
  • 18
0

OK. First of all, I don't know which angular version you are using, It might be helpful.

Secondly, I asume that you want 2 different controllers. Each contoller has its own $scope. If you want common funtions, you have 2 options:

1.(Recomended) Use an utils service or class 2. Declare them in the $rootScope. Then they will be accessible in the whole module (crudApp) by "$rootScope.getInfo();"

PD. Right now, you have declared only one controller. But I think you want to decalare 2 different controllers.

  • 1
    you should *never* recommend use of `$rootScope` for this; using `$rootScope` to share data across controllers is an anti-pattern. – Claies Aug 10 '16 at 12:13
  • Thank you for the advice :) It's good to be corrected to learn. Then, I recommend the service?, or what do you would do? – Enrique Fernandez Aug 10 '16 at 12:16
  • Yes, the entire reason that Services exist is to provide shared functionality. Over-using `$rootScope` leads to code that is hard to debug. – Claies Aug 10 '16 at 12:18
-1

Ideally, this should be done through event dispatching. If employeecontroller is a controller of crudApp (does not look so from the code snipped you shared), then it can be done through $rootScope.$broadcast and $scope.$on. If no so, it can be achieved by dispatching javascript events. Event will be listened in DbController.

Sumant
  • 1
  • 4