How to pass a value to an AngularJS $http success callback

Team AIGD

In my AngularJS application I am doing the following

$http.get('/plugin/' + key + '/js').success(function (data) {
    if (data.length > 0) {
        console.log(data);
        // Here I would also need the value of 'key'
    }
});

Now I need to access the key value within the success callback, i.e. I need to know which value it had when the get() request has been made.

Any "best practice" how to do so?

PS: I can do the following, but is there a better way?

var key = config.url.split('/')[2];
geniuscarrier

Solution 1:

$scope.key = key;
$http.get('/plugin/' + key + '/js').success(function (data) {
    if (data.length > 0) {
        console.log(data, $scope.key);
    }
});

Solution 2 (Updated per Jim Hong's observation in his answer):

$http.get('/plugin/' + key + '/js').success((function(key) {
    return function(data) {
        console.log(key, data);
    }
})(key));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass a value to an AngularJS $http success callback

From Dev

How to pass a value to an AngularJS $http then callback

From Dev

$scope variable value not accessible outside of $http success callback in AngularJS

From Dev

AngularJS - Manage the $http success callback

From Dev

AngularJS - Manage the $http success callback

From Dev

how to callback a method of controller in Angularjs $http.get().success()

From Dev

How to pass $(this) in success callback of $.ajax

From Dev

AngularJS : Scope issue in $http success callback

From Dev

angularJS $http.get() success callback not be called on cross domain

From Dev

AngularJS passing more than one value to promise's success callback

From Dev

How to pass argument to success callback in casperjs waitForSelector function?

From Dev

how to pass a variable to a Page Method Success callback function

From Dev

How to pass dynamic value to $http.post in angularjs

From Dev

AngularJS reject a promise in success callback

From Dev

Angularjs - $http success vs then

From Dev

How to pass data to http create server callback?

From Dev

Is it possible to return a value from the success method of $http.get in AngularJS?

From Dev

Is it possible to return a value from the success method of $http.get in AngularJS?

From Dev

pass value to success function

From Dev

How to pass parameters to $http in angularjs?

From Dev

Why Success or Error callback function not executing properly for $http.post in angularjs

From Dev

How to register callback with variadic arguments and pass a value

From Dev

pass callback to directive - angularjs

From Dev

How come success function is never called in $http.jsonp (angularjs)

From Java

Pass variable to function in jquery AJAX success callback

From Dev

AngularJS provide success callback of a factory method

From Dev

AngularJS post Success callback fired even on error

From Dev

AngularJS provide success callback of a factory method

From Dev

AngularJS $http callback not firing

Related Related

  1. 1

    How to pass a value to an AngularJS $http success callback

  2. 2

    How to pass a value to an AngularJS $http then callback

  3. 3

    $scope variable value not accessible outside of $http success callback in AngularJS

  4. 4

    AngularJS - Manage the $http success callback

  5. 5

    AngularJS - Manage the $http success callback

  6. 6

    how to callback a method of controller in Angularjs $http.get().success()

  7. 7

    How to pass $(this) in success callback of $.ajax

  8. 8

    AngularJS : Scope issue in $http success callback

  9. 9

    angularJS $http.get() success callback not be called on cross domain

  10. 10

    AngularJS passing more than one value to promise's success callback

  11. 11

    How to pass argument to success callback in casperjs waitForSelector function?

  12. 12

    how to pass a variable to a Page Method Success callback function

  13. 13

    How to pass dynamic value to $http.post in angularjs

  14. 14

    AngularJS reject a promise in success callback

  15. 15

    Angularjs - $http success vs then

  16. 16

    How to pass data to http create server callback?

  17. 17

    Is it possible to return a value from the success method of $http.get in AngularJS?

  18. 18

    Is it possible to return a value from the success method of $http.get in AngularJS?

  19. 19

    pass value to success function

  20. 20

    How to pass parameters to $http in angularjs?

  21. 21

    Why Success or Error callback function not executing properly for $http.post in angularjs

  22. 22

    How to register callback with variadic arguments and pass a value

  23. 23

    pass callback to directive - angularjs

  24. 24

    How come success function is never called in $http.jsonp (angularjs)

  25. 25

    Pass variable to function in jquery AJAX success callback

  26. 26

    AngularJS provide success callback of a factory method

  27. 27

    AngularJS post Success callback fired even on error

  28. 28

    AngularJS provide success callback of a factory method

  29. 29

    AngularJS $http callback not firing

HotTag

Archive