【问题标题】:How to check that the length of a string is equal to 0 in Angular JS如何在Angular JS中检查字符串的长度是否等于0
【发布时间】:2021-12-28 15:34:53
【问题描述】:

我声明并初始化了一个包含各种字段的数组,包括 targaauto。在添加功能中,我检查“如果车牌的长度等于 0,我会收到警报”错误!插入车牌! “。这一切都不能正常工作。事实上,如果我不输入车牌我看不到警报。除了代码中定义的条件之外,我还尝试了 if 内的以下条件

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

【问题讨论】:

  • 其实你为什么在这个问题中标记html和css?这甚至不是任何关系。
  • 因为我从 HTML 文件中获取值。在 HTML 文件中,变量定义如下

标签: javascript html css angularjs


【解决方案1】:

汽车是一个数组。因此,为了访问数组中某个项目的属性,首先需要选择该项目。例如,如果您想获取数组中第一项的 targaauto,请使用以下命令。

$scope.cars[0].targaauto  

并检查它的长度

$scope.cars[0].targaauto.length 

【讨论】:

  • 我做了你告诉我的所有测试,但它仍然不起作用
【解决方案2】:

在您的示例中,您从 $scope 变量添加到数组。问题似乎是您正在尝试检查 $scope.cars 数组中是否有零长度的车牌 - 但这不是问题 - 问题在于 $scope.targaauto 变量。在下面的小调整中,我添加了trim() 以消除任何空格,并添加了return alert... 以阻止函数将不完整的数据添加到汽车数组中。

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

【讨论】:

    猜你喜欢
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 2014-07-29
    • 2014-03-02
    • 2011-02-05
    • 2022-01-04
    相关资源
    最近更新 更多