-1
.controller("selectStudentController",function($scope,$http){
    $scope.showStudents = function(){
        $http.post("selectStudent.php").then(function(response){
            $scope.studentData = response.data;
        })
    }
})
.controller("saveStudentController",function($scope,$http){
    $scope.saveIt = function(){
        $http.post("saveNewStudent.php", {"sName" : $scope.sName,
                                          "gender" : $scope.gender,
                                          "salary" : $scope.salary,
                                          "dob" : $scope.dob}
        ).then(function(response){                            
            alert("Record Saved Successfully...");  
            $scope.showStudents();                            
        })                        
    }                   
})

Hi, this is my code. Here, when i call $scope.showStudents(); is not working after Record Saved Successfully... message. Can anybody tell me what is the mistake in it. My record saved and select fine but i unable to call this function.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
SRP
  • 29
  • 1
  • 9
  • you are doing wrong / your mistakes are: using `$scope`, putting obsolete quotes around object property names, using `alert` for debugging and having all controllers chained in one file. But the biggest mistake is asking question on stackoverflow before you understand your own code and calling it `function not working` which is describing 99% of programming problems – smnbbrv Oct 07 '17 at 07:17
  • Any console error ?? – Kaustubh Khare Oct 07 '17 at 07:17
  • why are you chaining the controllers? can you create a separate controller with different variables? like var app = angular.module([]); then app.controller("selectStudentController") and app.controller("saveStudentController") – Niladri Oct 07 '17 at 07:22
  • can you post the HTML also? – Niladri Oct 07 '17 at 07:22
  • everything is working fine... only $scope.showStudents() is not working after inserting the record. – SRP Oct 07 '17 at 07:26

2 Answers2

2

You have 2 controllers selectStudentController and saveStudentController

Suppose selectStudentController is not a parent scope of saveStudentController (otherwise your code should work)

You cannot call directly method of other controller from local one.

The best practice is to use service. Put method showStudents logic into service that will be available from both controllers.

app.service('YourService', ['$http', function ($http) {

    var self = this;

    self.showStudents = function(){
        return $http.post(
             "selectStudent.php"
          ).then(function(response){
               return response.data;
          })
       }    
  }]);

And now saveStudentController will look like:

.controller("saveStudentController",function($rootScope, $scope,$http,YourService){
                   $scope.saveIt = function(){
                        $http.post("saveNewStudent.php",
                        {"sName":$scope.sName,
                        "gender" : $scope.gender,
                        "salary" : $scope.salary,
                        "dob" : $scope.dob
                        }                                                                
                    ).then(function(response){                            
                        alert("Record Saved Successfully...");  
                        $rootScope.$broadcast('showStudents', {});
                    })                        
                }                   
            })

selectStudentController controller:

 .controller("selectStudentController",function($scope,YourService){

    $scope.$on("showStudents", function (event, data) {                    
      YourService.showStudents().then(function(response){
           $scope.studentData = response.data;
       })      
    });
  })

saveIt method you can also put into Service

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • thanks it is working fine. I don't know this. Recently i started to work on angularjs. but thanks once again i got new idea to create and call service. – SRP Oct 07 '17 at 07:45
1

You can not called directly local scope method in different controller

You have following way to access the methods in controller,

  1. Create same method in same controller
  2. Use services and inject in controller
  3. Use $rootScope(But not good idea)
  4. Use $broadcast and $emit

Sample code for you,

 .controller("saveStudentController",function($scope,$http){
                          //here declare your method
                          $scope.showStudents = function(){
                              $http.post(
                                  "selectStudent.php"
                                  ).then(function(response){
                                      $scope.studentData = response.data;
                                  })
                             }
                             $scope.saveIt = function(){
                                  $http.post("saveNewStudent.php",
                                  {"sName":$scope.sName,
                                  "gender" : $scope.gender,
                                  "salary" : $scope.salary,
                                  "dob" : $scope.dob
                                  }                                                                
                              ).then(function(response){                            
                                  alert("Record Saved Successfully...");  
                                  $scope.showStudents();                            
                              })                        
                          }                   
                      })

Please check same question to here.

Hope this will help you.

Santosh Shinde
  • 6,045
  • 7
  • 44
  • 68
  • This question already has an answer here https://stackoverflow.com/questions/29467339/how-to-call-a-function-from-another-controller-in-angularjs – Niladri Oct 07 '17 at 07:28