How to pass the contents of JSON Object to a controller

0

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

I'm currently testing 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 wonder, what's wrong with code that can not pass JSON content filled to the controller?

    
asked by anonymous 27.03.2017 / 23:41

1 answer

0

You have some problems with this function. If you use $http , which 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 aDadosEscalacao variable. Since you defined an empty object, it simply returns that object.

To return the correct value, you need to execute the get method that you set, which in this case populates the variable and returns with the correct value.

Just change

listaobjEquipes = EquipeService.listaobjEquipes;

To:

listaobjEquipes = EquipeService.get();
    
27.03.2017 / 23:58