Php Angularjs如何通过服务中的$http响应获取Json格式返回的解析数据

Php Angularjs如何通过服务中的$http响应获取Json格式返回的解析数据,php,mysql,angularjs,angularjs-directive,angularjs-scope,Php,Mysql,Angularjs,Angularjs Directive,Angularjs Scope,首先我想澄清的是,我不是在使用web服务访问数据。 我的数据库(php)和angularjs用户界面位于同一台服务器上 在AngularJs服务中,我向interface.php(数据库)发送http Get请求,它返回json格式。我不知道如何实际解析数据并将其发送给控制器 这里有明确的代码:) var app=angular.module(“app.chart.ctrls”['ngSanitize']) 控制器 app.controller("registrationCtrl",["$sc

首先我想澄清的是,我不是在使用web服务访问数据。 我的数据库(php)和angularjs用户界面位于同一台服务器上

在AngularJs服务中,我向interface.php(数据库)发送http Get请求,它返回json格式。我不知道如何实际解析数据并将其发送给控制器

这里有明确的代码:)

var app=angular.module(“app.chart.ctrls”['ngSanitize'])

控制器

 app.controller("registrationCtrl",["$scope","$location","logger","registerService",function($scope,$location,logger,registerService){

          $scope.data= registerService.getYears();
       **how to parse the data is it correct format or not ? in Controller** 
  } 

**Service**
app.factory('registerService', function ($http,$q,$log) {
 return {
        getYears:function () {


            var deferred = $q.defer();
           $http({
                 method : "GET",
             url : "interface.php", 

                 }).success(function(data){

                     **** How to Return the data from here to Controller ***

                 })

        },

    }
});
php

来自

资料来源:

[
 {
  "age": 13,
  "id": "motorola-defy-with-motoblur",
  "name": "Motorola DEFY\u2122 with MOTOBLUR\u2122",
  "snippet": "Are you ready for everything life throws your way?"
  ...
 },
...
]
您的服务看起来像:

phonecatServices.factory('Phone', ['$resource',
  function($resource){
    return $resource('phones/:phoneId.json', {}, {
      query: {method:'GET', params:{phoneId:'phones'}, isArray:true}
    });
  }]);
和您的控制器:

phonecatControllers.controller('PhoneListCtrl', ['$scope', 'Phone', function($scope, Phone) {
  $scope.phones = Phone.query();
  $scope.orderProp = 'age';
}]);

因此,您可以从控制器内调用服务并返回结果。

1-首先在控制器中定义一个对象,稍后您可以将其用作http响应的存储,如下所示:

         app.controller("registrationCtrl",["$scope","$location","logger","registerService",function($scope,$location,logger,registerService){

      $scope.data = {};
      // fire your servise function like this : 
      registerService.getYears($scope);
}

2-在您的服务中:

        app.factory('registerService', function ($http) {
        return {
         getYears:function (scope) {// scopes comes from your controller
        $http({method : "GET",url : "interface.php"})
        .success(function(data){
                scope.data = data;!!!!!!
             })
             }
          }
       });
到目前为止,它已经完成了,并且会起作用

但如果你想使用某种承诺,你可以这样做:

         app.controller("registrationCtrl",["$scope","$location","logger","registerService",function($scope,$location,logger,registerService){

      $scope.data = {};
      // fire your servise function like this : 
      registerService.getYears($scope);
在控制器中:

     .
     .
     .
     $scope.data = {};
      // fire your servise function like this : 
       var promise = registerService.getYears();
           promise.then(function(msg){
            $scope.data = msg.data[0];

           });
     .
     .
     .
为您服务:

  app.factory('registerService', function ($http) {
        return {
         getYears:function () {
          var promise =  $http({method : "GET",url : "interface.php"});
          }
           return promise ;
       });

deferred.resolve(JSON.parse(data))
然后在getYears函数的末尾
返回deferred.promise
尝试在getYears函数中返回$http promise,默认情况下为解析承诺。如果这不起作用,尝试使用
q
promissions.$data=[{“id”:1,“year”:1222},{“id”:2,“year”:343},{“id”:3,“year”:2323},];return json_encode($data)这是我的数据,从interface.php返回到服务,然后如何从服务发送到控制器,然后在控制器上根据列:)@JonathandeM访问。