我宣告并初始化了一個包含 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 = " ";
};
});
uj5u.com熱心網友回復:
汽車是一個陣列。因此,為了首先訪問陣列內專案的屬性,您需要選擇該專案。例如,如果您想獲取陣列中第一項的 targaauto,請使用以下命令。
$scope.cars[0].targaauto
并檢查其長度
$scope.cars[0].targaauto.length
uj5u.com熱心網友回復:
在您的示例中,您從 $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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404687.html
標籤:
