Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1


I declared and initialized an array with various fields including targaauto. In the add function, I check "If the length of the car plate is equal to 0 I get an alert" Error! Insert the license plate! ". All this does not work correctly. In fact, if I do not enter the license plate I do not see the alert. I have tried the following conditions inside the if in addition to the one defined in the code


START CODE ANGULAR JS

angular.module('tabelle', [])
.controller('test', function($scope){


$scope.cars =    [{id: "1", targaauto : "AR152FP", datiintestatario : "Maurizio Generosi", 
                 marca : 
                 "FIAT PUNTO", id_bottone: "1"},
                 {id: "2", targaauto  : "AR34512", datiintestatario : "Nicola Lops", marca : 
                 "TOYOTA YARIS", id_bottone: "2"},
                 {id: "3", targaauto : "BS25671", datiintestatario : "Sabrina De Martino", 
                 marca 
                 : "FIAT PANDA", id_bottone: "3"}];

$scope.aggiungi = function() {
    if($scope.cars.targaauto.length==0){
        alert("Errore! Inserire la targa");
    }
    $scope.cars.push({
    id: $scope.id,
    targaauto: $scope.targaauto,
    datiintestatario: $scope.datiintestatario,
    marca: $scope.marca,
    id_bottone: $scope.id_bottone
    })


    $scope.id = " ";
    $scope.targaauto = " ";
    $scope.datiintestatario = " ";
    $scope.marca = " ";
};


$scope.rigadaeliminare = function(indice) {
$scope.idcancellare = indice;

};
$scope.rimuovi = function () {
$scope.cars.splice($scope.idcancellare, 1);
};

//SELEZIONE INDICE DELLA RIGA DEL RECORD
function rigadamodificare(indice){
for(let i=0; i<$scope.cars.length;i++){
    if($scope.cars[i].id==indice){
        return i;
    }
}
return -1;
};

$scope.aggiorna = function(id) {
let index = rigadamodificare(id);
let i = $scope.cars[index];
$scope.id=i.id;
$scope.targaauto=i.targaauto;
$scope.datiintestatario=i.datiintestatario;
$scope.marca=i.marca;
};

$scope.salva = function() {
let index = rigadamodificare($scope.id);
$scope.cars[index].targaauto = $scope.targaauto;
$scope.cars[index].datiintestatario = $scope.datiintestatario;
$scope.cars[index].marca = $scope.marca;

        $scope.id = " ";
        $scope.targaauto = " ";
        $scope.datiintestatario = " ";
        $scope.marca = " ";
};
});


What I have tried:

PROVEN CONDITIONS

if (cars.targaauto.length == 0)

if ($ cars.targaauto.length == 0)

if (targaauto.length == 0) 
Posted
Updated 29-Dec-21 11:46am

1 solution

Hi,

you're using a lot of " " which is not an empty string, it is a string holding one space, so its length is 1.

So either change those to "" or test for a larger length (e.g. whatever the minimum length for a plate is in your world).

:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900