nour nour nour nour - 1 year ago 65
AngularJS Question

How to get the url's value of $http.get() to use inside the function?

Hi I parsed three different web services using AngularJS: Here's my code:

app.factory('myapp', ['$http', function($http) {
function getLists() {
var tab = ['url1', 'url2', 'url3'];
var list = [];
for(i = 0; i < tab.length; i++) {
.then(function(res) {'';
}).catch(function (res) {'file not found';
return list;

return {
getLists: getLists

In an html page I have to show the data found in Json files
<tr ng-repeat="d in list" ng-show="d.error == ''">

<tr ng-repeat="d in list"ng-show="d.error != ''">
<td>File not found</td></tr>

So if the URL is not found or there is an error I should show a line where I put error.
What I want now is tp specify the Url not found. For example if url1 is not found, I want to put a message url1+" is not found"
I tried to do this in my factory tab[i]+' file not found';

and in my html :

<tr ng-repeat="d in list"ng-show="d.error != ''">

but nothing appeared and when I used an alert to debug

alert(tab[i]+' file not found');

I got 'undefined file not found'.
Is there a way to solve my problem?

Answer Source

use the response object, res.config.url contains the url which was hit. But then, as the url did not exist, the .catch block will be executed and not .then block

.catch(function(res) { = { error : res.config.url +' file not found' };

also, you will have to make the calling code use it asynchronously, right now you are trying to access in synchronous way, which will never work...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download