Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数在angularjs中不工作_Javascript_Angularjs - Fatal编程技术网

Javascript 函数在angularjs中不工作

Javascript 函数在angularjs中不工作,javascript,angularjs,Javascript,Angularjs,嗨,这是我的密码。在这里,当我调用$scope.showStudents()时

嗨,这是我的密码。在这里,当我调用
$scope.showStudents()时代码不起作用…消息。谁能告诉我里面有什么错误吗。我的记录已保存并选择“罚款”,但我无法调用此功能。

您有两个控制器
selectStudentController
saveStudentController

假设
selectStudentController
不是
saveStudentController
父作用域(否则代码应该可以工作)

您不能直接从本地控制器调用其他控制器的方法

最佳做法是使用
服务
。将方法
showStudents
逻辑投入服务,这两个控制器都可以使用

.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();                            
        })                        
    }                   
})
现在,
saveStudentController
看起来像:

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

    var self = this;

    self.showStudents = function(){
        return $http.post(
             "selectStudent.php"
          ).then(function(response){
               return response.data;
          })
       }    
  }]);
选择学生控制器
控制器:

.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', {});
                    })                        
                }                   
            })


saveIt
方法您也可以投入使用

您不能在不同的
控制器中直接调用本地
范围
方法

您可以通过以下方式访问控制器中的方法:

  • 在同一控制器中创建同一方法
  • 使用服务和注入控制器
  • 使用$rootScope(但不是好主意)
  • 使用
    $broadcast
    $emit
  • 您的示例代码

     .controller("selectStudentController",function($scope,YourService){
    
        $scope.$on("showStudents", function (event, data) {                    
          YourService.showStudents().then(function(response){
               $scope.studentData = response.data;
           })      
        });
      })
    
    请勾选相同的问题


    希望这能对您有所帮助。

    您做错了/您的错误是:使用
    $scope
    ,在对象属性名称周围加上过时的引号,使用
    警报进行调试,并将所有控制器链接到一个文件中。但是最大的错误是在你理解你自己的代码之前就提出了关于stackoverflow的问题,并称它为
    函数不起作用
    ,它描述了99%的编程问题一个控制台错误??你为什么要链接控制器?你能用不同的变量创建一个单独的控制器吗?比如var-app=angular.module([]);然后app.controller(“selectStudentController”)和app.controller(“saveStudentController”)也可以发布HTML吗?一切正常。。。插入记录后,只有$scope.showStudents()不起作用。此问题已在此处找到答案,谢谢。它工作正常。我不知道这个。最近我开始研究angularjs。但再次感谢您,我有了创建和呼叫服务的新想法。
     .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();                            
                                  })                        
                              }                   
                          })