Execute a function when loading the page, Javascript and Angular

Asked

Viewed 7,717 times

0

I am making a registration page, and I need to relate a collaborator to a company, but for that I need when the page load I already have a companies in a list>

I’m already getting the companies on a list, problem and make the companies object to be loaded as soon as the page loads.

This is my function of Angular:

  $scope.colaborador = {};
    $scope.empresas = {};

    //Lista todas empresas para cadastro do colaborador
    $scope.getEmpresas = function () {
        var url = "cadastro-colaborador-empresa";
        MainService.get(url).then(function (cb) {
            location.href = 'cadastro-colaborador';
            $scope.empresas = cb;
        });
    };


    $scope.cadastrarColaborador = function () {
        var url = 'cadastra-colaborador';
        MainService.post(url, {colaborador: $scope.colaborador}).then(function (cb) {
            if (cb.idPessoa !== undefined) {
                location.href = 'index';
                $scope.colaborador = {};
                $scope.empresas = {};
            } else {
                $scope.alertaErro = cb;
                $("#myAlert").show();
            }
        });
    };

I’m using vRaptor and Java too, the function cadastra-colaborador-empresa returns the company list for the enterprise object.

I tried to call the page so <a href="${pageContext.request.contextPath}/administrador/cadastro-colaborador" ng-click="getEmpresas()">Cadastrar Colaborador</a> But it didn’t work, my select this way

 <select ng-options="empresa.idEmpresa as empresa.nome for empresa in empresas" ng-model="colaborador.empresa"></select>

The idea is to have the companies loaded in this select,.

  • Why don’t you call the function $scope.getEmpresas() on your controller?

  • I’m starting in Angular and javascript I don’t know much, so within the angular controller itself I can already call the function ? I’ll try here.

2 answers

2


You can call your function within itself Controller, that way it will be called as soon as the page opens.

EX:

angular.module('myApp', []);

angular.module('myApp')
  .controller('myCtrl', function($scope) {
    $scope.buscar = function() {
        $scope.nome = 'Nome Teste';
    }
    //Chamando a função buscar
    $scope.buscar();
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
 {{nome}}
</div>

  • 1

    Thank you very much, it worked perfectly, I am new in Angular and Javascript, my notion sometimes disturbs me to make simple things, thanks for the help, now I can solve many other things that were problem in my code.

  • Good luck and good studies.

0

Try using a script tag just below your body:

<script>
  document.ready(function(){
    $scope.getEmpresas();
  });
</script>

Although it’s not the only solution :)

Browser other questions tagged

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