How to pass JSON Object content to a controller

Asked

Viewed 267 times

0

How do I pass the contents of the service JSON object to controller.

At the moment I do tests with the following codes:

Controller(app.js)

  example.controller('equipecontrol', ['EquipeService','$scope', function( EquipeService,$scope) {      

Parse.initialize('APPLICATION_ID', 'JAVASCRIPT_KEY'); 
Parse.serverURL = 'https://parseapi.back4app.com';

EquipeService.$inject = ['EquipeService']; // Injetamos o service

equipes =  { "nomejogador": '',
             "posioriginal":'',
             "nomedotime": '',
             "jogoude": ''
            };

listaobjEquipes = EquipeService.listaobjEquipes;   <--- NESTE MOMENTO O OBJETO ESTÁ RETORNANDO VAZIO

...

Service(service.js)

var example = angular.module('starter')
example.service('EquipeService', ['$http', function($http) {
  var aDadosEscalacao = {};
  return { listaobjEquipes: aDadosEscalacao,
        get: function(param) {
          var nteste = 1
          if (neste = 1) {
            idjogador = '1';
            idtime = '1';
            idjogoude = '1';
            idpelada = '1';
            ngolsjogador = '207';
            faltastxtJSON = '1';
            ncrtamarelos = '1';
            ncrtvermelhos = '1';
            nptoscraque = '18';
            nptosdestaque = '19';
            nptosmaster = '21';
            nptosgoleiro = '12';
            aDadosEscalacao = { "idjogador": idjogador, 
                                "idtime": idtime,
                                "idjogoude": idjogoude,
                                "idpelada": idpelada,
                                "gols": ngolsjogador,
                                "faltas": faltastxtJSON,
                                "cartao_amarelo": ncrtamarelos,
                                "cartao_vermelho": ncrtvermelhos,
                                "pontuacaocraque": nptoscraque,
                                "pontuacaodestaque": nptosdestaque,
                                "pontuacaomaster": nptosmaster,
                                "pontuacaogoleiro": nptosgoleiro };
          }
          return aDadosEscalacao
        }
  }
}])

I ask, what is wrong with the code that cannot pass the content of JSON completed for the controller?

1 answer

0


You have some problems in this function. If you use $http, that you inject but do not use, you will have asynchronous problems, but let’s focus on the current problem.

You are using a method of your service that simply returns the value of the variable aDadosEscalacao. As you define an empty object, it simply returns that object.

To return the correct value, you need to run the method get which you have defined, which in this case fills the variable and returns with the correct value.

Just change

listaobjEquipes = EquipeService.listaobjEquipes;

To:

listaobjEquipes = EquipeService.get();
  • Okay Leandro, it just worked by implementing get() as you directed. I would also like to record another form that was made and worked as well: it was following the guidelines of the video posted on this link https://www.youtube.com/watch?v=i2_k4ECZrTI the code went like this ... https://www.youtube.com/watch?v=i2_k4ECZrTI and in the controller listobjEquipes = Equipeservice.getEscalacao(); I decided to leave registered only to add our knowledge bank. In short I find your answer cool, because I don’t need to change anything in service.js, just in the controller. Thanks again!

  • good afternoon! after this solution found, I edited my codes and implemented parse.query instructions in service.js, the method I am using now is getEscalation, however it cannot transmit the contents of the aDadosEscalation object to the controller.

  • @Ita opens another issue with the full and updated code there. It’s easier to understand the problem.

  • I already did, follow the link http://answall.com/questions/193295/como-passar-um-conte%C3%bado-de-json-object-to-a-controller-part-2-using-pair

Browser other questions tagged

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