2016-06-21 7 views
0

AngularJSアプリケーション内に表示するデータベースから従業員のリストを取得する機能を実装しています。データ検索はコントローラ内では機能しますが、サービスを使用しないときは - AngularJS

$scope.getEmployee = function() { 
     $http.get("EmpWebService.asmx/GetEmp") 
     .then(function (response) { 
      $scope.employees = response.data; 
     }); 
    } 

Iは、サービス内で使用するために、このロジックを移動しようとしたが、データがundefinedを返して:私は下に表示される現在の作品がコントローラを作成しました。これは数日前にうまくいたので奇妙ですが、突然それは動作していないようです。ここでは、コードされていますので、$ scope.employeesは約束ではない従業員

コントローラ

$scope.getEmployee2 = function() { 
     $scope.employees = fetchEmpService.fetchEmp(); 
    } 

Service.js

fetchEmp: function() { 
      debugger; 
      return $http.get("EmpWebService.asmx/GetEmp") 
       .then(function (response) { 
        return response.data; 

       }); 
     } 
+1

あなたのサービスに '$ http'を注入しましたか? –

答えて

2

fetchEmpは約束を返しますので、上記のコードは動作しませんですリストしようとすると、

$scope.getEmployee2 = function() { 
    fetchEmpService.fetchEmp().then(function (employeeList){ 
     $scope.employees = employeeList; 
    }); 
} 
+0

ありがとう、それは私のソリューションです –

1

fetchEmpService.fetchEmp()は約束を返します。成功したコールバックでこのサービスからemployeesオブジェクトにアクセスできます。

$scope.getEmployee2 = function() { 
    fetchEmpService.fetchEmp() 
    .then(function(emp){ 
     $scope.employees = emp; 
    }); 
} 
関連する問題