2017-09-23 50 views
-1

我正在尝试从JSON文件中读取名称。我首先使用唯一的控制器创建没有服务的服务,然后将业务逻辑转移到服务。我没有清楚地知道如何将$ scope.mydata从服务返回给控制器。 没有得到任何输出,请帮助。谢谢。在AngularJS中使用服务将对象值返回给控制器

 <!DOCTYPE html> 
     <html> 
     <head> 
     <title></title> 
     <!-- Add angularjs --> 
     <!-- Add controller script--> 
     <script type="text/javascript"> 
      var app = angular.module('x2js', []); 
      app.controller('ctrl', 
       ['$scope','myService',function($scope,myService){ 
       $scope.data = function(){ 
        myService.getData() 
       } 
      }]); 
      app.service('myService', ['$http','$log', function($http,$log){ 
      this.getData = function(){ 
       $http({ 
        url:"student.json", 
        method:"GET", 
        dataType: 'json', 
        contentType: "application/json" 

       }).then(function(response){ 
        $scope.myData = response.data.records; 
        $log.info($scope.post); 
        return($scope.myData); 
       },function(response){ 
        $log.error("Error occured"); 
       }); 
      } 
     }]) 
     </script> 
     </head> 
     <body ng-app="x2js"> 
     <div ng-controller="ctrl"> 
      <table> 
       <tr> 
        <td ng-repeat="x in Data">{{x.Name}}</td> 
       </tr> 
      </table> 
     </div> 
    </body> 
    </html> 

回答

0

在服务,return.then方法创建的承诺:

app.service('myService', ['$http','$log', function($http,$log){ 
    this.getData = function(){ 
     ͟r͟e͟t͟u͟r͟n͟ $http({ 
      url:"student.json", 
      method:"GET", 
      //dataType: 'json', 
      //contentType: "application/json" 
     }).then(function(response){ 
      var myData = response.data.records; 
      //$log.info($scope.post); 
      return(myData); 
     },function(errorResponse){ 
      $log.error("Error occured "+errorResponse.status); 
      //IMPORTANT 
      throw response; 
     }); 
    } 
}]) 

在控制器中,提取从承诺数据:

app.controller('ctrl', 
    ['$scope','myService',function($scope,myService){ 
     var promise = myService.getData(); 

     promise.then(function(data) { 
      $scope.data = data; 
     }).catch(errorResponse) { 
      console.log(errorResponse,status); 
      throw errorResponse; 
     }); 

    }]); 

的的.then方法承诺返回一个新的承诺通过0的返回值解决或拒绝,errorCallback(除非该值是承诺,在这种情况下,它将使用promise chaining中的承诺中解决的值解决。 1

在承诺的错误处理程序,重要的是要重新抛出错误响应,否则拒绝许将转换到履行承诺返回的undefined的值。

2

使用promise$http的服务回报承诺。您需要将您的then功能移到控制器中。

<!DOCTYPE html> 
     <html> 
     <head> 
     <title></title> 
     <!-- Add angularjs --> 
     <!-- Add controller script--> 
     <script type="text/javascript"> 
      var app = angular.module('x2js', []); 
      app.controller('ctrl', 
       ['$scope','myService', '$log',function($scope,myService, $log){ 

        myService.getData().then(function(response){ 
        $scope.data = response.data.records; 
        $log.info($scope.data); 

       },function(response){ 
        $log.error("Error occured"); 
       }) 
       } 
      }]); 
      app.service('myService', ['$http', function($http){ 
      this.getData = function(){ 
       retutn $http({ 
        url:"student.json", 
        method:"GET", 
        dataType: 'json', 
        contentType: "application/json" 

       }); 
      } 
     }]) 
     </script> 
     </head> 
     <body ng-app="x2js"> 
     <div ng-controller="ctrl"> 
      <table> 
       <tr> 
        <td ng-repeat="x in data">{{x.Name}}</td> 
       </tr> 
      </table> 
     </div> 
    </body> 
    </html> 
+0

仍然没有输出。 :( –

+1

@AmitPhartiyal你使用'Data'到html和控制器的值分配给'data'变量 –

+0

其实它是myData在他的例子中 – pegla

相关问题