Using angular and Ionic, how do I traverse a vector by summing a variable of an object?

Asked

Viewed 390 times

0

I have a vector of objects with prices, product name..., after the customer has clicked on a product I pass it to another list, of orders, after being done this need to make me add up the prices of the products of the list and return me a total each time q a new product is added and if removed needs to be subtracted from the total. I’m using $Scope in this application because it’s an Ionic app.

I’m using a service to fill this list because I have two more vectors q have the msm specifications but divide the products into two more vectors to better organize and it was the solution I found. But I don’t understand what it represents very well in the code.

.controller('pedidoCtrl', function($scope, produtoService) {

    $scope.items = [];
    $scope.items = produtoService.getProdutos();

    $scope.deleteItem = function(item){
        $scope.items.splice($scope.items.indexOf(item), 1);
    };
})

.service('produtoService', [function(){
    var produtosLista = [];

    var addProduto = function(produto) {
        produtosLista.push(produto);
    };

    var getProdutos = function(){
        return produtosLista;
    };

    return {
        addProduto: addProduto,
        getProdutos: getProdutos,
    };
}]);
  • 2

    Your question is not quite clear. Try to click [Edit] and improve it a bit. Explain it calmly: 1. What do you intend to do? (The expected result) 2. What is exactly the problem you’re having? 3. Have you ever tried something? If so, what? And, how? And what was the problem you had with your attempt?

  • vlw by tip, tried to improve

No answers

Browser other questions tagged

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