How to pass a controller variable to the service?

Asked

Viewed 130 times

1

I have a question, as I pass to a service a controller variable?

I have a screen that has a table, when the user clicks on a row of this table, it goes to another screen with the data filled in inputs. I did a search on the internet, and I saw some people suggesting to record this variable in a service, so I could call in another controller, but I could not make it work:

User click on selected line:

inserir a descrição da imagem aqui

when clicking, the screen changes, and the data (Active type and maturity) selected should appear in the following inputs:

inserir a descrição da imagem aqui

Follow the code of what I was trying to do:

Table page (test table):

template:

<table class="table table-hover table-bordered">
                    <thead>
                        <tr>
                            <th>

                            </th>
                            <th>
                                Tipo Ativo
                            </th>
                            <th>
                                Emissor
                            </th>
                            <th>
                                Código
                            </th>
                            <th>
                                Emissão
                            </th>
                            <th>
                                Vencimento
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-click="$ctrl.teste()">
                            <td>
                                1
                            </td>
                            <td id="teste">
                                TB - Monthly
                            </td>
                            <td>
                                Default
                            </td>
                            <td>
                                Default
                            </td>
                            <td>
                                01/04/2012
                            </td>
                            <td>
                                01/04/2012
                            </td>
                        </tr>
                </table>

Controller:

(function() {
'use strict';

class PesquisaAtivosController {
  constructor($state, $scope, testeService){
    this.$state = $state;
    this.testeService = testeService;
  }

  teste(){
    var ItemSelecionado = document.getElementById('teste');
    console.log(teste);
    var _this = this;
    _this.$state.go("home.boletoEstoque2");
  }
}

PesquisaAtivosController.$$ngIsClass = true;
PesquisaAtivosController.$inject = ['$state','testeService'];

angular.module('app')
  .controller('PesquisaAtivosController', PesquisaAtivosController);})();

Page that should receive the data:

Template: I just created a button with the function of appearing this variable in the console

controller:

(function () {
'use strict';

class BoletoEstoqueController {
constructor($state, $scope, testeService) {
  this.$state = $state;
  this.$scope = $scope;
  this.testeService = testeService;
}

teste(){
  testeService.getItem();
  console.log(ItemSelecionado );
}

}

BoletoEstoqueController.$$ngIsClass = true;
BoletoEstoqueController.$inject = ['$state', 'testeService'];

angular.module('app')
  .controller('BoletoEstoqueController', BoletoEstoqueController);
})();

Service:

(function () {
'use strict';

function testeService($http, $q) {

    var ItemSelecionado = document.getElementById('teste');

    return {
        getItem: function () {
            return ItemSelecionado;
        },
        setItem: function(value) {
            ItemSelecionado = value;
        }
    };

}

testeService.$inject = ['$http', '$q'];

angular.module('app')
    .service('testeService', testeService)
})();
  • I don’t understand well, if you are going to use a service in the controller, you cannot pass by parameter when running some service Function?

  • I want to save a variable to the service, to call this service in another controller and use this variable

  • I’ll put some prints on the question

  • If it’s code, put it instead of print!

  • Okay, I put some prints and codes from where I left off

  • @guilhermedjc this table, you have this data in a javascript object? because if you have it, you can pass this object as a parameter in the service. So any controller that accesses the service, will be accessing the same data that is being displayed in your table.

  • No, not yet, this table I made for test even putting the data directly in the template.

  • Create a variable at the top of your service, then create a Function that reads/writes value into that variable. In the first controller call this Function by passing the value you want and in the other call the Function that returns the value, see this example: https://stackoverflow.com/a/20181543/4730201 another solution is to use broadcast

  • Are you using ui-router or something?

  • @guilhermedjc first step is you put this data into an object, create this object to change your example using it, so I can help you create the service and the controller properly.

  • Beauty Rodrigo. Yes Marcus, I’m using ui-router

  • @guilhermedjc a hint, when making a comment directed to the place our nickname after @, as I did with yours. So I get a notification that there was a message for me.

Show 7 more comments
No answers

Browser other questions tagged

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