Home > Mobile >  How to check that the length of a string is equal to 0 in Angular JS
How to check that the length of a string is equal to 0 in Angular JS

Time:12-29

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

PROVEN CONDITIONS

if (cars.targaauto.length == 0)

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

if (targaauto.length == 0) 

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 = " ";
};
});

CodePudding user response:

cars is an array. therefore, in order to access a property of an item inside the array first you need to select the item. For example, if you want to get targaauto of the first item in your array use the following.

$scope.cars[0].targaauto  

and to check its length

$scope.cars[0].targaauto.length 

CodePudding user response:

In your example, you're adding to an array from $scope variables. The issue appears to be you're trying to check the $scope.cars array for a zero-length plate - but that isn't the concern - the concern is the $scope.targaauto variable. In the small adjustment below, I added trim() to get rid of any spaces, and a return alert... to stop the function from adding the incomplete data to the cars array.

$scope.aggiungi = function() {
  if($scope.targaauto.trim().length==0){
    return alert("Errore! Inserire la targa");
  }
  // .... rest of function
  • Related