1

Sorry to ask something like this, but i m already spend couple of days to solve this one. But i m really need help. I am already read Understanding Dependency Injection and also other stack question use $http inside custom provider in app config use the $injector on run block also not work in my code.

Perhaps someone here can help me, i am also worried how annotate the injection on my provider for minification.

My code in 

Here

Community
  • 1
  • 1
Adi Prasetyo
  • 1,006
  • 1
  • 15
  • 41

1 Answers1

0

The problem is that when you call $http in the _getItem you are in the "controller". A solution could be to do this:

  this.$get = function($http) {
    return {
      mainUrl : _mainUrl,
      getItem : _getItem,
      httpWrapper : $http
    };

And then in the _getItem assign (or use the httpWrapper directly):

var $http = this.httpWrapper;

In order to avoid problems with the minification, you could add the injection to the method $get:

  this.$get =   ['$http', function($http) {

Here the updated Plunker: http://plnkr.co/edit/m7oXNolo6iV8Zy1NJ0vx

EDIT: I think that what you wanted to achieve is this:

  var _getItem = function() {
    var _d = null;
            console.log(this)
    var $http = this.httpWrapper;
    var $q = this.qWrapper;
     _d = $q.defer();
    $http.get(_mainUrl)
    .success(function(data) {
      console.log(data);
      _d.resolve( {data: data, x: "efe"} );
    })
    .error(function(status) {
      _d.resolve( status );
    });
    return _d.promise;
  };

$q.defer() allows to defer the http call to your controller, then on your controller you have to edit this:

facade.getItem().then(function(data){
  $scope.myReturn = data;
}

Updated Plunker: http://plnkr.co/edit/xR1d3KnWhX51jHHZ8t3m

Note: Consider that the nature of then is asynchronous, thus if you use the variable straight afterwards, it still will be undefined

Michael
  • 3,308
  • 5
  • 24
  • 36
  • ok thanks dude, it works. But can you tell and give me example again? Because when i want create a defered object on the local scope, it's got undifined. i want 1 defered object on the local scope. – Adi Prasetyo Jan 09 '15 at 05:44
  • ok thanks, i mark it. But any reference? i got problem when try make costume callback(error and success) on the service and call it on the controller (attach the respone or error message to $scope) – Adi Prasetyo Jan 10 '15 at 11:20
  • For $q and defer, you can check this: http://lostechies.com/gabrielschenker/2014/02/04/angularjspart-11-promises/. – Michael Jan 12 '15 at 09:18