0

First of all i want to clear, That am not accessing the data using web service. My database(php) and angularjs UI are on the same server it self.

In Service of AngularJs, am sending http Get Request to interface.php(Database) it return json format. I dont how to actually parse the data and send it to Controller ?

Here Clear Cut Code :)

var app=angular.module("app.chart.ctrls",['ngSanitize']);

Controller

 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 ***

                 })

        },

    }
});

interface.php

3
  • deferred.resolve(JSON.parse(data)) then at the end of the getYears function return deferred.promise Commented Jun 19, 2014 at 12:02
  • Try to return $http promise in getYears function, angular resolve promises by default. If that will not work try to use q promises. Commented Jun 19, 2014 at 12:04
  • $data = [{"id":1,"year":1222},{"id":2,"year":343},{"id":3,"year":2323},]; return json_encode($data) This is my data that return from interface.php to service Then how to send from service to controller, Then at Controller how to access according to Column :)@JonathandeM. Commented Jun 19, 2014 at 12:16

2 Answers 2

1

1 - First define a object in your controller that later you can use as a storage for your http response like this :

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

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

}

2- In your Servise :

        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;!!!!!!
             })
             }
          }
       });

It's done so far and it'll work ;

BUT if your want to use some kind of promise , you can do like this :

in your controller :

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

           });
     .
     .
     .

in your Service :

  app.factory('registerService', function ($http) {
        return {
         getYears:function () {
          var promise =  $http({method : "GET",url : "interface.php"});
          }
           return promise ;
       });
Sign up to request clarification or add additional context in comments.

Comments

0

from https://docs.angularjs.org/tutorial/step_11

the source:

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

your service looks like:

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

and your controller:

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

so, you call the service from within the controller and get the results back.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.