I have a question, how do I step into 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 have searched the internet, and I saw some people suggesting to record this variable in a service, to be able to call in another controller, but I could not make it work:
User clicks on selected line:
Whenyouclick,thescreenchanges,andthedata(activetypeandexpiration)selectedshouldappearinthefollowinginputs:
HereisthecodeIwastryingtodo:
Tablepage(testtable):
template:
<tableclass="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 you should receive the data:
Template: just created a button with the function to appear 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)
})();