1

I can see my json data in the console and I want to view it on html page after clickbutton function. From my understaning I can either do a promise ($q) or then with http or ngResource. First I want to do http then migrate to ngResource. For some reason my scope is still undefined. Maybe it's a ng-init or ng-repeat I'm missing? Any ideas?

  var app = angular.module('myApp', []);

  app.factory('httpq', function($http, $q) {
  return {
    get: function() {
      var deferred = $q.defer();
      $http.get.apply(null, arguments)
      .success(deferred.resolve)
      .error(deferred.resolve);
      return deferred.promise;
    }
  }
});

  app.controller('myController', function($scope, httpq) {

  httpq.get('http://localhost:8080/states')
  .then(function(data) {
    $scope.returnedData = data;
  })

  $scope.clickButton = function() {
      $scope.returnedData;
}

});

view

   <div data-ng-controller="myController">
        <button data-ng-click="clickButton()">Get Data From Server</button>
        <p>JSON Data : {{returnedData}}</p> 

    </div>
2
  • i will advice you to go with the ajax service in angualr. Commented May 28, 2015 at 11:51
  • you know how to accept answer meta.stackexchange.com/questions/5234/… Commented Oct 21, 2015 at 8:28

4 Answers 4

2

Use Ajax call

Service:

var demoService = angular.module('demoService', [])
.service('myService',['$http', function($http) {

    this.getdata = function(entity){
        var promise = $http({
            method : 'POST',
            url : 'services/entity/add',
            data : entity,
            headers : {
                'Content-Type' : 'application/json'
            },
            cache : false
        }).then(function (response) {
            return response;
        });
        return promise;     
    };
}]);

Controller :

var demoService = angular.module('demoService', [])
.controller('myctr',['$scope','myService',function($scope,myService){
   myService.getdata().then(function(response){
            //Success

        },function(response){

            //Error         
        });

}]);

now you can see your json in controller success

Sign up to request clarification or add additional context in comments.

1 Comment

you can also use .factory instead of .service its just an working example for you :D
1

$http itself is a promise, no need to create a new promise. Just return the $http.get wihoit the success written there and right the sucess fn in the controller itself. So your code will look like this:

app.factory('httpq', function($http) {
   return {
       get: function() {
          return $http.get.apply(null, arguments);
       }
   }
});

Your controller:

app.controller('myController',  function($scope, httpq) {
      httpq.get('http://localhost:8080/states').then(function(data) {
   $scope.returnedData = data;
 })

  $scope.clickButton = function() {
      $scope.returnedData;
  }

 });

4 Comments

Thanks V. That works. Quick question. When someone gives a solution to your problem am I supposed to answer my own question? Is there a way to mark the question as complete?
Yes there is a tick mark against the answer on the left just click it
If you're talking about the up and down arrows, my reputation isn't high enough yet. Guess I have to wait? Thanks. I have to read stackoverflow's rules. Maybe find some basic java questions to answer first.
You can mark this answer as accepted with clicking the tick besides this answer below the arrow
0

use

$scope.returnedData=JSON.parse(data);

It will give you values in JSON format

Comments

0

I have not worked with promise. But your factory code seems to be ok.

In controller declare your object first.

If it's just object declare it as

$scope.returnedData = {};

If it's array, declare it as

$scope.returnedData = [];

The the object will not be undefined and changes will affect in HTML

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.