$ Http What should I use?

3

The $http has two examples. I would like to help me because I do not know which is the most useful

There is the:

var chamada = function () {
    return $http.get('https:url/exemplo.json')
        .then(function (response) {
            return response.data;
         });
    }

and o:

 var outraChamada = function () {
     return $http.get('https:url/outroExemplo.json')
         .success(function (data) {
             return data;
         }).error(function (status) {
             return status;
         })
     }

Which one should I choose? Is there a way to return the date if it is changed in the Web Service (without having to call the function)?

    
asked by anonymous 25.02.2016 / 18:39

2 answers

3

Use then because as @lbotinelly said success and error are deprecated .

$http.get('https:url/exemplo.json')
    .then(function(response) {
        return response.data;
    }, function(err) {
        console.log(err);
    });
  

Is there any way I can return the date if it is changed in the   Web Service (without having to call the function)?

A: No, your page does not know "alone" if something has changed on the backend. There are a few alternatives, one of which is to create a function that checks every x minutes and makes sure something has changed, in which case you can use $ timeout .

    
25.02.2016 / 20:40
1

$ http.get () returns a Promise object, which allows us to chain functions as if they were synchronous.

The then threaded function accepts two arguments: a successful handler and an error handler .

The correct way is to use this format available in documentation (the success and error are deprecated ):

$http.get('/someUrl', config).then(successCallback, errorCallback);

Example:

var chamada = function () {
    return $http.get('https:url/exemplo.json').then(this.sucesso, this.erro);
}

this.sucesso = function(response) {
    return response.data;
}

this.erro = function(error) {
    console.log('Não foi possível obter os dados: ' + error.data);
}
    
25.02.2016 / 20:46