2017-10-07 81 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