Request GET with AngularJs

1

Good afternoon I'm trying to get a Get request with AngularJs but I do not receive any response and the only console.log that executes is the last that is already outside the request;

        angular.module("listaTelefonica").controller("listaTelefonicaCtrl", function ($scope, $http) {
        $scope.contatos = [];
       var carregarContatos = function(){
          $http({method:'GET',url: 'http://www.json-generator.com/api/json/get/ccLAsEcOSq?indent=1'}).then(function(response){
            console.log("conexão foi realizada") 
            console.log(response.data);

          }).catch(
             console.log("falha na conexão")
          );
          console.log("falha na conexão")
       };
       console.log("falha na conexão")
    
asked by anonymous 10.12.2018 / 16:20

2 answers

1

Try this:

var url = 'http://www.json-generator.com/api/json/get/ccLAsEcOSq?indent=1';
var carregarContatos = function(){
      return $http.get(url).then(function(response){
        console.log("conexão foi realizada") 
        console.log(response.data);

      }).catch(
         console.log("falha na conexão")
      );
      console.log("falha na conexão")
   };
   console.log("falha na conexão")
    
10.12.2018 / 16:27
0

If you have passed your code by some transpiler, such as Babel, or minifier, you should always use the declared services in the creation of the controller.

And you forgot, that first comes the name of the controller, and then comes the array of services. Missed the " [" ] "

angular.module("listaTelefonica").controller("listaTelefonicaCtrl", ["$scope", "$http", function ($scope, $http) {

//Seu codigo aqui

}]);
    
10.12.2018 / 16:30