2

In most angularjs tutorials I've seen promise chains result in changing a scope variable.

$http.get(someURL).then(function (value) {
    $scope.someValue = value;
});

Is it possible to return that value to a parent function? Ever method I've tried just returns another promise.

$scope.test = function(){
    return $http.get(someURL).then(function (value) {
        return value;
    });
};

$scope.test = function(){
    var deferred = $q.defer();
    $http.get(someURL).then(function (value) {
        deferred.resolve(value);
    });
  return deferred.promise;
}

I don't want to chain another promise, just return the json so I can do something like this:

<button ng-click='some_vars = test()'>Get ajax</button>
<ul ng-repeat='var in some_vars'>
    <li>{{var.title}}</li>
</ul>
3
  • 1
    No, because you can't return from AN AJAX call - you can still set a scope variable to the result of an AJAX call and the view will auto update. Commented Oct 17, 2014 at 19:46
  • @tymeJV that's not true, depends on the version that the OP is using, promise unwrapping has been deprecated and I would never advice anyone to do that, but in Anular 1.2 it's still possible to change the settings of the $parseProvider, like this: $parseProvider.unwrapPromises(true); Commented Oct 17, 2014 at 20:34
  • embed a defer inside a promise is an antipattern github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns i know that's on the server side, but you can take it as a reference Commented Oct 17, 2014 at 20:34

1 Answer 1

3

IMO this is how you should do it:

In your controller do this:

$scope.some_vars=[];
$scope.loadVars = function(){
    $http.get(someURL).success(function (data) {
        $scope.some_vars=data;
    });
};

In your view do this:

<button ng-click='loadVars()'>Get ajax</button>
<ul ng-if="some_vars.length>0" ng-repeat='var in some_vars'>
    <li>{{var.title}}</li>
</ul>

Much easier, right?

However if you are using a version of Angular prior to 1.2.0-rc.3. and you really want to automatically unwrap the promises (I would strongly advise you against that), you would have to change the settings of the $parseProvider in the config of your module, like this:

.config(function($parseProvider){
  $parseProvider.unwrapPromises(true);
})

Working Example

Promise unwrapping was removed with version 1.2 and it has been completely deprecated with version 1.2.0-rc.3.

You may want to have a look at this document: Migrating from Previous Versions.

$parse: due to fa6e411d, promise unwrapping has been removed. It has been deprecated since 1.2.0-rc.3. It can no longer be turned on. Two methods have been removed:

     $parseProvider.unwrapPromises 

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

3 Comments

I get that you can do it this way, but it is truly IMPOSSIBLE to just return the value? I want to avoid having to make a bunch of different functions that control how to update the scope, prefer to just do it in the html.
@BillJohnston no, it's not impossible, I will update my answer in a sec, but I would advise you against it.
@BillJohnston I've updated my answer, please have a look at it, thanks!

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.