Angular assign data from a POST request

Asked

Viewed 82 times

0

Good people I have the following code

app.controller('lista', function($http){
        $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
        //this.feriados = <?php echo json_encode($feriados->feriados); ?>;

        this.feriados = {};

        $http({
            method: 'POST',
            url: '../controllers/feriados.php',
            data: "op=listaTodos"
        }).then(function (respondse){
            this.feriados = response.data;
            console.log(this.feriados);
        },function (error){

        });


       });

My problem is that in the print for the console the "holidays" appear, but if do <div ng-controller="lista as l"> {{ l.feriados }}</div> I see that the variable keeps the value {}.

1 answer

0


I discovered the mistake :)

Is in the assignment to the variable this, I’ve misjudged your range.

Just add inside the controller

var controller = this;

And then assign the answer to this variable

controller.feriados = resposta;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.