2017-05-22 4 views
-1

サーバーからデータを取得しているファイルはservices.jsです。私はメインのスクリプトメソッドのフォームのサービス層にデータを取得していないが、メインのスクリプトファイルがあります。別途サービスでWeb APIを使用して作業中にデータを取得していません

BLOCKQUOTE

コードを確認してください、と私は間違いを持っ​​ているなら、私に知らせてください。

app.factory("studentServices", function ($http)## 



---------- 


## 
return { 

     postData: function (student) { 

      $http.post("/api/Student/Register", student) 
       .then(function (response) { 
       console.info(response.data); 

       debugger; 
       return response.data; 

      }, function (error) { 

       console.info("error in reruest"); 
      }); 
     }, 
     getData: function() { 
      debugger; 
      var rt; 
      $http.get("/api/Student/getStudent").then(function (response) { 
       console.info(response.data); 
       debugger; 
       rt=response.data; 
      }, function (error) { console.info("error in geting"); }) 
      debugger; 
      return rt; 
     } 

     } 
    }) 

/// <reference path="../Scripts/angular.js" /> 
/// <reference path="script.js" /> 
var app = angular.module("myModule", []).controller("myController", function ($scope, $http, studentServices) { 
    debugger; 
    console.info("init"); 

    $scope.addStudent = function() { 
     console.info("just"); 
     console.info($scope.id); 
     $scope.result=""; 
     var student = {id:$scope.id,name:$scope.name,email:$scope.email,college:$scope.college,mobile:$scope.mobile} 
     console.info(student); 
     // $http.post("/api/Student/Register", student).then(function (response) { 
     // console.info(response.data); 
     $scope.result = studentServices.postData(student); 
      console.info("result " + $scope.result); 
      get(); 
     // }, function (error) { console.info("error in reruest") }) 
     //$scope.getStudents(); 
    } 


    $scope.getStudents = function get() { 
     debugger; 
      console.info("gettinf studetn list"); 
     // $http.get("/api/Student/getStudent").then(function (response) { 
     //  console.info(response.data); 
      var local = studentServices.getData(); 
      debugger; 
      $scope.students = local; //response.data; 
      debugger; 
      console.info("getdata list "); 

    //  }, function (error) { console.info("error in geting") }) 

     } 
     console.info("last"); 

}) 
+0

ファクトリメソッドを呼び出す場所? – anoop

答えて

0
"ここの工場のための他のserviseがあります"

以下のものを修正する必要があります:

1.工場出荷時のメソッドでは、apiの約束を返します。例えば:あなたのコントローラ2.In

postData: function (student) { 
    return $http.post("/api/Student/Register", student); 
} 

は、ファクトリメソッドとその応答を消費します。たとえば :

studentServices.postData(student).then(function (result){  
    $scope.result = result.data; 
}, function(error){ 
    // handle error 
}) 

getDataについて同様の操作を行います。

$httpは非同期なので、コントローラ内のデータを解決する必要があります。したがって、データを取得してください。これを参照してくださいfiddle

+0

なぜ私たちはそれを返す必要があるのですか? – friend

+0

@teja: '$ http is asynchronous'のため、$ scopeは呼び出しが完了する前にデータを受け入れています。ヌルです。したがって、$ scope内のデータはありません。データを割り当てるためにコントローラーでそれを解決する必要があります。 – anoop

+0

@teja:これを見てください[fiddle。](http://jsfiddle.net/owj5chz8/) – anoop

関連する問題