2017-10-07 80 views
-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();        
     })       
    }     
}) 

嗨,这是我的代码。在这里,当我拨打$scope.showStudents();记录已保存成功...消息。有人可以告诉我它有什么错误吗?我的记录保存并选择正常,但我无法调用此函数。函数不工作在angularjs

+0

你做错了/你的错误是:使用'$ scope',在对象属性名称周围放置过时的引号,使用'alert'进行调试并将所有控制器链接在一个文件中。但是最大的错误是在你理解你自己的代码并将其称为“功能不工作”(它描述了99%的编程问题)之前,在stackoverflow上提出问题 – smnbbrv

+0

任何控制台错误? –

+0

你为什么要链接控制器?你能用不同的变量创建一个单独的控制器吗?像var app = angular.module([]);然后app.controller(“selectStudentController”)和app.controller(“saveStudentController”) – Niladri

回答

1

不能直接调用本地scope方法不同controller

,可以有以下方式访问控制方法,

  1. 在同一个控制器创建同样的方法
  2. 使用服务,并在控制器
  3. 注入
  4. 使用$ rootScope(但不是好主意)
  5. 使用$broadcast$emit

为你的示例代码,

.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();        
           })       
          }     
         }) 

请检查同样的问题here

希望这会帮助你。

+0

这个问题已经在这里有一个答案https://stackoverflow.com/questions/29467339/how-to-call-a-function-from-another-controller-in-angularjs – Niladri

2

你有2个控制器selectStudentControllersaveStudentController

假设selectStudentController不是父范围的saveStudentController(否则你的代码应工作)

不能调用从本地另外一个控制器的直接方法。

最佳做法是使用service。将方法showStudents逻辑放入将从两个控制器可用的服务中。

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

    var self = this; 

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

现在saveStudentController看起来像:

.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("selectStudentController",function($scope,YourService){ 

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

saveIt方法你也可以投入服务

+0

感谢它工作正常。我不知道这一点。最近我开始研究angularjs。但是再次感谢我提出了创建和致电服务的新想法。 – ShailendraRPhadke