Right now, I am using $http.get on an online json data source. In my controller:
$http.get('source').success(function(data) {
$scope.nextPageToken = data.nextPageToken;
$scope.prevPageToken = data.prevPageToken;
});
Getting the variable $scope.nextPageToken works fine. My HTML code for displaying it (for testing)
<p>{{nextPageToken}}</p>
works and it displays correctly in the browser. But when I try to put use the variable $scope.nextPageToken in the controller
$http.get('source').success(function(data) {
$scope.nextPageToken = data.nextPageToken;
$scope.prevPageToken = data.prevPageToken;
});
$http.get('source&pageToken=' + $scope.nextPageToken).success(function(data) {
$scope.picture = data.thumbnail.url;
});
The console in the console returns a bad request (400). It seems to be trying to reach the link 'source&pageToken=undefined', which means that $scope.nextPageToken doesn't get evaluated or doesn't get passed to the $http.get parameter.
This is strange because URL is correct when I define a string variable and concatenate it to the URL of the $http.get parameter. For example, it works when I try
this.key = 'someKey';
$http.get('source&key=' + this.key).success(function(data) {
$scope.nextPageToken = data.nextPageToken;
$scope.prevPageToken = data.prevPageToken;
});
Could this be because I am trying to access a variable that is only accessible inside the $http.get function block? If so, how do I access the $scope.nextPageToken variable outside the block?
Any help is appreciated. Thanks!
edit (after basilikum's answer): I have also tried
this.someKey = 'stringKey';
$http.get('source&key=' + this.someKey).success(function(data) {
$scope.nextPageToken = data.nextPageToken;
$scope.prevPageToken = data.prevPageToken;
$http.get('source&pageToken=' + $scope.nextPageToken + '&key' + this.someKey).success(function(data) {
$scope.picture = data.thumbnail.url;
});
});
(this is a more specific example of the actual code I have) When I do this though, the error that I get is
TypeError: Cannot read property 'someKey' of undefined
I had already tried the nested $http.get inside another one, but I get another error completely, so I thought it was just creating more problems. Could anyone tell me what is going on here?
thisyou use outside of the function and thethisyou use inside of the function are not the same. Please read this (pun not intended) developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…