How to pass the result of an angular promise JS to the scope of Angular JS?

0

I am a beginner with the subject of promises in Javascript. I'm doing an Http query to a laravel method that brings me a little list. When doing the query all ok, it shows me the records in console.log, but when trying to make them available to the scope of angular, I throw error. I show the complete code, of the promise, that works ok and of the code that calls the execution of the promise and tries to assign the result to the scope of angular js, (which is where the fault is)

    //-------------------------------------------
    //PROMESA QUE HACE HTTP A UN METODO Y TRAE REGISTROS VARIOS. FUNCIONA CORRECTAMENTE
    function get(url) {
      // Return a new promise.
      return new Promise(function(resolve, reject) {
        // Do the usual XHR stuff
        var req = new XMLHttpRequest();
        req.open('GET', url);

        req.onload = function() {
          // This is called even on 404 etc
          // so check the status
          if (req.status == 200) {
            // Resolve the promise with the response text
            resolve(req.response);
          }
          else {
            // Otherwise reject with the status text
            // which will hopefully be a meaningful error
            reject(Error(req.statusText));
          }
        };

        // Handle network errors
        req.onerror = function() {
          reject(Error("Network Error"));
        };

        // Make the request
        req.send();
      });
    }

    //----------------------------------------
    //EJECUTO LA PROMESA Y TRAE RESULTADOS OK:
    get('listadoregistros').then(function(response,$scope) {
    //INTENTO ASIGNAR EL RESULTADO DE LA PROMESA AL $SCOPE DE ANGULAR
    //console.log(response); //(aqui me trae ok todos los datos)
    $scope.listado=response;

    }, function(error) {
      console.error("Failed!", error);
    })
    //-------------------------------------------
    //AQUI ESTA EL PROBLEMA
    //este console.log falla con Uncaught (in promise) TypeError: Cannot set property 'listado' of undefined
    console.log($scope.listado);
    /*
    
asked by rendor9 02.09.2018 в 16:24
source

0 answers