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];
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.
Comments