2017-01-24 7 views
0
  1. 私のサービスではなく、コントローラではなくhttpを使用しますか?
  2. 私は、$ scopeでデータを表示しようとすると、私のconsole.logで定義されていません。

ここに私のコード

app.controller('adminControl', ['$scope','$routeParams','$route','adminService', function($scope,$routeParams,$route,adminService){ 
    $scope.data = adminService.listOfAdmin(); 
    console.log($scope.data); 
}]).service('adminService', ['$http', function($http){ 
    this.get = function(url){ 
     return $http({ 
      method:'GET', 
      url:url 
     }).then(function(response){ 
      return response; 
     }); 
    } 

    this.listOfAdmin = function(){ 
     this.get('http://localhost/project/s9/ayu/admin/sys/mac.php?act=administrator') 
      .then(function(response){ 
       return response.data; 
      }); 
    } 
}]); 

答えて

0

あなたは約束を返し、その約束をfullfilledされるのを待たなければなりません。

app.controller('adminControl', ['$scope','$routeParams','$route','adminService', function($scope,$routeParams,$route,adminService){ 
    $scope.data = adminService.listOfAdmin().then(function(response) { 
     console.log(response.data); 
    }); 

}]).service('adminService', ['$http', function($http){ 
    this.get = function(url){ 
     return $http({ 
      method:'GET', 
      url:url 
     }); 
    } 

    this.listOfAdmin = function(){ 
     return this.get('http://localhost/project/s9/ayu/admin/sys/mac.php?act=administrator'); 
    } 
}]); 
+0

iが出力を得ました。 ng-repeat broでどのように表示するのですか? –

+0

$ scope.myArray = response.data; ng-repeat = "n in myArray" ?? – yBrodsky

+0

はい、問題なく動作しますが、コントローラを変更しました: adminService.listOfAdmin()。(機能(応答){ $ scope.data = response.data; }); Thnks –

0

それは約束を返し、約束が解決された後のデータが設定されるべきである:私はconsole.logにアレイ[オブジェクト]:

app.controller('adminControl', ['$scope','$routeParams','$route','adminService', function($scope,$routeParams,$route,adminService){ 
    adminService.listOfAdmin().then(function(data) { 
     $scope.data = data; 
     console.log($scope.data); 
    }); 

}]).service('adminService', ['$http', function($http){ 
    this.get = function(url){ 
     return $http({ 
      method:'GET', 
      url:url 
     }).then(function(response){ 
      return response; 
     }); 
    } 

    this.listOfAdmin = function(){ 
     return this.get('http://localhost/project/s9/ayu/admin/sys/mac.php?act=administrator') 
     .then(function(response){ 
      return response.data; 
     }); 
    } 
}]); 
+0

console.logにObject {$$ state:Object}という出力があります。そして確信している。私はこの出力を私のng-repeatテーブルに入れることを納得した –

関連する問題