Calculation using Angular JS

1

IgothelpfromEsmigoltosolveoneoftheproblemsandI'minsistingherebecauseIdidnotfindanythingontheinternetthatcouldhelpme.TherearefewexamplesofwhatIneedontheinternet.Thelinecalculationisok(quantityxunitvalue)plusthesumandsubtractionoftheTotalAmountIamcatching.WhatamIdoingwrong[![

$scope.valorClaro = [
        {id: 1, gb: 0, qtd: '', preco: 39.99, descricao: "Ligações Ilimitadas", operadora: "Tim"},
        {
            id: 2,
            gb: 0.5,
            qtd: '',
            preco: 45.99,
            descricao: "Ligações Ilimitadas + WhatsApp + SMS + Conteúdo Digital",
            operadora: "Claro"
        }
    ]

    $scope.soma = 0;

    $scope.multiplicaValor = function (index) {
        index.totalLinha = index.qtd * index.preco;
        angular.forEach($scope.valorClaro, function () {
            $scope.soma += index.qtd * index.preco;
        });
        console.log(index.totalLinha);
    }
<form name="formSimula">
    <div class="card mt-3">
        <div class="card-header font-weight-bold">
            <img src="./dist/images/icon-claro.png"> Simulador Planos Claro
            <span class="float-right" style="font-size: 30px;">{{soma | currency}}</span>
        </div>

        <table class="table table-hover mb-0" style="font-size: 0.875em;">
            <thead>
            <tr class="alert-warning d-flex">
                <th class="text-center col-1">GB</th>
                <th class="col-2">Valor</th>
                <th class="col-5">Descrição</th>
                <th class="col-2 text-center">Qtd Linhas</th>
                <th class="col-2">Total</th>
            </tr>
            </thead>
            <tbody>
            <tr class="d-flex font-weight-bold font-open" ng-repeat="claro in valorClaro">
                <td class="align-middle text-center col-1">{{claro.gb}}</td>
                <td class="col-2 align-middle">{{ claro.preco | currency}}</td>
                <td class="align-middle col-5">{{ claro.descricao}}</td>
                <td class="align-middle col-2 text-center">
                    <input type="text" class="form-control form-control-sm text-center" ng-model="claro.qtd"
                           ng-change="multiplicaValor(claro)">
                </td>
                <td class="align-middle col-2">
                    <input type="text" class="form-control form-control-sm text-center"
                           value="{{claro.totalLinha | currency}}">
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</form>

] 2 2

    
asked by anonymous 13.06.2018 / 20:42

1 answer

0

You do not need to put an id in the input to get the value typed, add in your array plus a variable in the case below created with the name of quantity, then in the input place as ng-model this variable and in its function of multiplying pass the index of the registry, you will have access to the data in the function and you can perform the multiplication of the line you want.

angular.module('App').controller("AppCtrl", function($scope, $http) {
  $scope.valorClaro = [{
      id: 1,
      gb: 0,
      quantidade: 0,
      preco: 39.99,
      descricao: "Ligações Ilimitadas",
      operadora: "Claro",
totalLinha: 0
    },
    {
      id: 2,
      gb: 0.5,
      quantidade: 0,
      preco: 45.99,
      descricao: "Ligações Ilimitadas + WhatsApp + SMS + Conteúdo Digital",
      operadora: "Claro",
totalLinha: 0
    }
  ];

  $scope.totalLinha;
  $scope.multiplicaValor = function(index) {
    index.totalLinha = index.quantidade * index.preco;
  }
});
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script></head><body><table><tbody><trclass="d-flex font-weight-bold font-open" ng-repeat="claro in valorClaro">
        <td class="align-middle text-center col-1">{{claro.gb}}</td>
        <td class="col-2 align-middle">{{ claro.preco | currency}}</td>
        <td class="align-middle col-5">{{ claro.descricao}}</td>
        <td class="align-middle col-2 text-center">
          <input type="text" class="form-control form-control-sm text-center" id="linha" ng-model="claro.quantidade" ng-change="multiplicaValor(claro)">
        </td>
        <td class="align-middle col-2">
          <input type="text" class="form-control form-control-sm text-center" ng-model="claro.totalLinha">
        </td>
      </tr>
    </tbody>
  </table>



</body>

</html>
    
13.06.2018 / 22:57