I have a very simple html page which I'm using a bit of Angular JS. Each field has it's enable/disable roll. On the especific field INCLUIR SEGURO, I call a method called ValidaIncluirSeguro which is the root of my problem. This method validades the value of the car based on a hardedcode value and depending upon the value it must disable Incluir Seguro field and show an alert warning the user that the value of the car is too expensive. My problem is that at the very first time I chose the values Tipo = Automóvel (Default). Marca = HONDA, Modelo = CIVIC, Ano, 2010 the alert is called twice. I know it's because the digest cycle but how could I fix it? I don't want it to be prompted twice.
Can anyone tell me how could I get it fixed?
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var app = angular.module('mainApp', []);
app.controller('mainController', function ($scope) {
$scope.ValidaIncluirSeguro = function (veiculo) {
try {
if (!veiculo.ano)
return true;
var valorMaximo = 15000000;
//VALIDO O VALOR DO VEÍCULO
var bloqueiaDevidoAoValor = $scope.ValidaValorMaximo(veiculo.ano.valor, valorMaximo);
if (bloqueiaDevidoAoValor) {
alert("Veículo fora da regra de aceitação do seguro devido ao valor do veículo de R$ " + (FormatarReal(veiculo.ano.valor)).toString() + ". Venda disponível apenas do rastreador com apoio a localização.");
//veiculo.incluirSeguro = "0";
return true;
}
return false;
} catch (e) {
alert("Erro no método [ValidaIncluirSeguro]. Detalhes do erro: " + e.message);
}
}
$scope.ValidaValorMaximo = function (valorVeiculo, valorMaximo) {
if (valorVeiculo > valorMaximo)
return true
else
return false;
}
//DADOS MOCADOS - ESTES DADOS DEVERÃO SER SUBSTITUÍDOS PELOS DADOS DO WEBSERVICE
$scope.DadosMocados = function () {
$scope.fipe = [
{
nome: 'HONDA', modelos: [{
nome: "CIVIC", codigoFipe: 1, anos: [{ ano: 1995, valor: 15000000 }, { ano: 2009, valor: 15100000 }, { ano: 2010, valor: 16000000 }]
},
{
nome: "CITY", codigoFipe: 2, anos: [{ ano: 2013, valor: 12300 }, { ano: 2014, valor: 13300 }, { ano: 2015, valor: 14300 }]
},
{
nome: "FIT", codigoFipe: 3, anos: [{ ano: 2015, valor: 12500 }, { ano: 2016, valor: 13500 }, { ano: 2017, valor: 14500 }]
}]
},
{
nome: 'FIAT', modelos: [{
nome: "BRAVO", codigoFipe: 4, anos: [{ ano: 2016, valor: 12800 }, { ano: 2017, valor: 13800 }, { ano: 2018, valor: 14800 }]
},
{
nome: "UNO", codigoFipe: 5, anos: [{ ano: 1990, valor: 12900 }, { ano: 1991, valor: 13900 }, { ano: 1992, valor: 14900 }]
},
{
nome: "SIENA", codigoFipe: 6, anos: [{ ano: 2001, valor: 12100 }, { ano: 2002, valor: 13100 }, { ano: 2003, valor: 14100 }]
}]
},
{
nome: 'BMW', modelos: [{
nome: "X1", codigoFipe: 7, anos: [{ ano: 2015, valor: 20000 }, { ano: 2016, valor: 30000 }, { ano: 2017, valor: 40000 }]
},
{
nome: "X6", codigoFipe: 8, anos: [{ ano: 2010, valor: 50000 }, { ano: 2011, valor: 60000 }, { ano: 2012, valor: 70000 }]
},
{
nome: "320i", codigoFipe: 9, anos: [{ ano: 2012, valor: 120000 }, { ano: 2013, valor: 220000 }, { ano: 2013, valor: 330000 }]
}]
},
];
}
//ONLOAD
$scope.DadosMocados();
});
function FormatarReal(int) {
try {
var tmp = int + '';
tmp = tmp.replace(/([0-9]{2})$/g, ",$1");
if (tmp.length > 6)
tmp = tmp.replace(/([0-9]{3}),([0-9]{2}$)/g, ".$1,$2");
return tmp;
} catch (e) {
alert("Erro no método [FormatarReal]. Detalhes do erro: " + e.message);
}
}
</script>
<table class="table table-sm">
<thead>
<tr>
<th width="15%">Tipo</th>
<th width="20%">Marca</th>
<th width="40%">Modelo</th>
<th width="15%">Ano</th>
<th width="10%">Incluir Seguro</th>
</tr>
</thead>
<tbody>
<tr>
<!----------------------------TIPO---------------------------->
<td>
<select ng-model="veiculos[0].tipo" ng-init="veiculos[0].tipo='1'" class=" form-control">
<option value="1">Automóvel</option>
<option value="2">Moto</option>
<option value="3">Caminhão</option>
</select>
</td>
<!----------------------------MARCA---------------------------->
<td>
<select ng-model="veiculos[0].marca" ng-options="marca as marca.nome for marca in fipe" class="form-control">
<option></option>
</select>
</td>
<!----------------------------MODELO---------------------------->
<td>
<select ng-model="veiculos[0].modelo" ng-disabled="HasValue(veiculos[0].marca)" class="form-control" ng-options="modelo as modelo.nome for modelo in veiculos[0].marca.modelos">
<option></option>
</select>
</td>
<!----------------------------ANO---------------------------->
<td>
<select ng-model="veiculos[0].ano" ng-disabled="HasValue(veiculos[0].modelo)" class=" form-control" ng-options="ano as ano.ano for ano in veiculos[0].modelo.anos">
<option></option>
</select>
</td>
<!----------------------------INCLUIR SEGURO---------------------------->
<td>
<!--<select ng-model="veiculos[0].incluirSeguro" ng-disabled="ValidaIncluirSeguro(veiculos[0])" ng-init="veiculos[0].incluirSeguro='1'" class=" form-control">-->
<select ng-disabled="ValidaIncluirSeguro(veiculos[0])" class=" form-control">
<option value="1">Sim</option>
<option value="0">Não</option>
</select>
</td>
</tr>
</tbody>
</table>