The problem is that you are using the $rootScope
in the html
. The ideal is not to use $rootScope
and yes the this
to share data with view
. You’re also using the push
in the array
, therefore you cannot access your variable by name, but by a position. Change your controller
to the following:
angular
.module('meumodulo',[])
.controller('MercadoriaCarrinhoController', MercadoriaCarrinhoController);
MercadoriaCarrinhoController.$inject = ['$http'];
function MercadoriaCarrinhoController($http){
var vm = this;
vm.listaDoCarrinho = {};
_iniciar();
function _iniciar() {
var mercadoria = {
id: 'id',
setor: 'setor',
foto: 'foto',
descr: 'descr',
de: de,
por: por,
mercadoria: '0',
quantidade: 0,
total: '5',
boto: -1
}
vm.listaDoCarrinho.mercadoria = mercadoria;
}
}
And the div
in his html
:
<div class="body" ng-controller="MercadoriaCarrinhoController as vm">
<span>Total dos produtos: {{vm.listaDoCarrinho.mercadoria.total}}</span>
</div>
If you want to keep implementing by array
change its implementation to the following:
angular
.module('meumodulo',[])
.controller('MercadoriaCarrinhoController', MercadoriaCarrinhoController);
MercadoriaCarrinhoController.$inject = ['$http'];
function MercadoriaCarrinhoController($http){
var vm = this;
vm.listaDoCarrinho = [];
_iniciar();
function _iniciar() {
var mercadoria = {
id: 'id',
setor: 'setor',
foto: 'foto',
descr: 'descr',
de: de,
por: por,
mercadoria: '0',
quantidade: 0,
total: '5',
boto: -1
}
vm.listaDoCarrinho.push(mercadoria);
}
}
And the div
in his html
:
<div class="body" ng-controller="MercadoriaCarrinhoController as vm">
<span>Total dos produtos: {{vm.listaDoCarrinho[0].total}}</span>
</div>
Use the vm
only for the variables you need printed on the screen, otherwise use var
.
Friend, an observation: In the last line you declared . push($rootScope.mercadoria0); but two variable is rootScope merchandise..
– Israel Zebulon
Thanks for the warning, it was a typo. It’s $rootScope.mercadoria0 and I’m not getting it anyway...
– Gymo
Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).
– Sorack