Resolved - Angularjs + Pagseguro - The list does not update through the callback

0

I am trying to make a listing of the types of payments available in the pagseguro api.

To do this I use a js function of the pagseguro called PagSeguroDirectPayment.getPaymentMethods ()

The problem is that in the callback of this function, I can not access my angular variables. When I create a normal variable it can access. If I put a button to put the data of the normal variable in the angular one it works, but I wanted it to load in the function callback.

                var self = this;
                this.lista = [];

                PagSeguroDirectPayment.getPaymentMethods({
                    success: function(response) {                           
                        self.lista.push({"nome": "Cartão de Crédito", "img":"../static/img/cartao.png", "objpag": response.paymentMethods.CREDIT_CARD});
                        self.lista.push({"nome": "Débito Online", "img":"../static/img/debito.png", "objpag": response.paymentMethods.ONLINE_DEBIT});
                        self.lista.push({"nome": "Boleto", "img":"../static/img/barcode.png", "objpag": response.paymentMethods.BOLETO});
                    },
                    error: function(response) {
                        //tratamento do erro
                    },
                    complete: function(response) {
                    }

                });
    
asked by anonymous 20.02.2018 / 16:01

1 answer

0

For the binding to work on a callback I have to force it. For this to be possible there is the function $ scope. $ Apply () .

So in my code it would have to look like this:

            var self = this;
            this.lista = [];

            PagSeguroDirectPayment.getPaymentMethods({
                success: function(response) {                           
                    self.lista.push({"nome": "Cartão de Crédito", "img":"../static/img/cartao.png", "objpag": response.paymentMethods.CREDIT_CARD});
                    self.lista.push({"nome": "Débito Online", "img":"../static/img/debito.png", "objpag": response.paymentMethods.ONLINE_DEBIT});
                    self.lista.push({"nome": "Boleto", "img":"../static/img/barcode.png", "objpag": response.paymentMethods.BOLETO});
                    $scope.$apply();
                },
                error: function(response) {
                    //tratamento do erro
                },
                complete: function(response) {
                }

            });

Whoever wants more information can access the link: link

To leave the solution here to help anyone who ever needs it, because this stackoverflow pt never helped me at all.

    
20.02.2018 / 21:26