Controller by file in angular js

Asked

Viewed 50 times

1

(function () {
    'use strict';

    modulo
            .controller("CarregaCamposController", CarregaCamposController)
            .service("CarregaCamposService", CarregaCamposService);

    function CarregaCamposController($scope, CarregaCamposService) {

        var dados;
        var tab;
        var campos = [];

        dados = CarregaCamposService.getJson();

        dados.forEach(function (val, key) {
            if (key != 0) {// posição 0 do json sempre é preenchida com o nome da fabrica de relatórios
                //montagem da tabela de campos para montagem do relatório
                if (tab === "" || tab != val.descricao[0]) { // Gera uma linha classificando os campos entre suas tabelas
                    campos.push({id: val.id[1], desc: val.descricao[1], tab: val.descricao[0], ct: "1"});
                    tab = val.descricao[0];
                } else {
                    campos.push({id: val.id[1], desc: val.descricao[1], tab: val.descricao[0], ct: "0"});
                }
            }
        });

        $scope.campos = campos;

        $scope.insereRemove = function (id) {
            alert(id);
        };

    }



    function CarregaCamposService($http) {

        //service para trazer os dados do json de campos do relatório
        this.getJson = function(){
            this.file = $http.get('json.php').then(function (result) {
                if (result.status == 200) {
                    return result.data;
                } else {
                    alert("Erro ao carregar o arquivo JSON!");
                }

            });
        };
    }

})();

within the service the http return result.data is correct, an array of obejtos, if I return it to a variable within the controller it already looks like undefined

That’s the first mistake

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

1 answer

0

First check if you imported the script of your controller in a tag <script in the html. Change the statement of your controller to find the module registered in AngularJS by name. After that state the function you are using to build your controller and not just its content. These guidelines will be more or less as follows:

//main.js
(function () {
  angular.module('Relatorio',[]);
})();

//make_controller.js
(function () {
  'use strict';

  angular
    .module('Relatorio')
    .controller('CarregaCamposController', CarregaCamposController);

  CarregaCamposController.$inject = ['$scope'];

  function CarregaCamposController($scope) {
      $scope.teste = 'teste 2';
  }
})();

//form_controller.js
(function () {
  'use strict';

  angular
    .module('Relatorio')
    .controller('FormularioController', FormularioController);

  FormularioController.$inject = ['$scope'];

  function FormularioController($scope) {
    function CarregaCamposController() {
      $scope.teste = 'teste 2';
    }
  }
})();
  • worse than the problem is with the expensive file, I was editing the file, I was inspecting it by the browser ta coming empty file, it must be pq use netbeans or something with apache, to editing the file and saving but the browser not, it may be tbm bug in smartGit

  • I cleaned the navigation data, now that is not working anything msm kkkkkk

  • @HUGOPABLO add one Minimum, Complete and Verifiable your question.

Browser other questions tagged

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