Answer from http.get in Scope

2

I'm writing a driver in AngularJS that calls http.get to an API, and returns a 0 (for now). I can show the 0 by console without problems, but I can not show it in a $ scope in the front.

App.js

   .controller('ssnGenAltaCtrl', ['$scope', '$http',
    function($scope, $http){
        $scope.data = {};
        $scope.generarRubricaAlta = function(data){
        $http({
            method: 'GET',
            url: 'url'
            }).then(function successCallback(data) {
                console.log(data.data);
                $scope.data.mensaje = data.data;
            }, function errorCallback(data) {
                console.log("Error");
            });
        }
    }]);

HTML

<div>
    <a class="btn btn-default" href="#" role="button" ng-click="generarRubricaAlta()">Generar Rubrica Alta</a>
    <p class="bg-primary">{{data.mensaje}}</p>
</div>

Routing

.when("/ssnGenAlta", {
    templateUrl : "views/ssnGenAlta.html",
    controller: "ssnGenAltaCtrl"
})

The controller I have declared it in the routing. You are not giving me any errors right now by console, but when you click on the button it shows me the 0 returned by console but not in the front.

    
asked by Tomas Pichierri 30.12.2016 в 14:12
source

2 answers

0

These two links may be of help (same web):

link

and

link

If it does not work for you, I have two other options, each one more dense ..

Option 1:

Generate a variable when entering the controller

angular.module('aplicativo').controller('MainCtrl', function ()
{
    var main = this;
    ...

and use this variable as scope

$http({
   method  : 'POST',
   url     : 'url',
   data    : 'data'
})
.success(function(data){
     main.user = 'Pepito';
 })
 .error(function() {
     console.error('ERROR!');
  });

and in the view:

    <div>{{main.user}}</div>

Option 2:

In the AJAX response, a callback function, something like:

function in the controller:

$scope.getAct(
    function (data) {
          console.log(data)
    }
);

AJAX call function:

$scope.getAct = function(functionCallback)
{

   $http({
      method  : 'POST',
      url     : 'url',
      data    : 'data'
   })
   .success(function(data){
        functionCallback(data);
   })
   .error(function() {
        console.error('ERROR!');
   });
    
answered by 09.01.2017 в 11:28
0

By any chance in the application config do you have an http interceptor? Something like the following:

$httpProvider.interceptors.push(function () {

});

In which you are intercepting the responses and you are not returning them properly?

    
answered by 14.03.2017 в 06:36