我試圖避免重復,不允許添加空專案。如果我再次發送相同的標題(例如,哈利波特),我不希望它第二次添加到 $scope.libri 中。添加前如何檢查?這是我現在的代碼:
角度 .module('myApp', []) .controller('myCtr', function ($scope){
$scope.libri = [{id:'1',titolo:'Harry Potter',genere:'Fantasy',anno:'2001',autore:'J.K.Rowling'},
{id:'2',titolo:'Il nome della rosa',genere:'Giallo',anno:'1994',autore:'Umberto Eco'},
{id:'3',titolo:'Cado dalle nubi',genere:'Commedia',anno:'2014',autore:'Checco Zalone'}];
$scope.add = function () {
$scope.libri.push({
id: $scope.id,
titolo: $scope.titolo,
genere: $scope.genere,
anno: $scope.anno,
autore: $scope.autore
});
$scope.id = ' ';
$scope.titolo = ' ';
$scope.genere = ' ';
$scope.anno = ' ';
$scope.autore = ' ';
};
uj5u.com熱心網友回復:
只需$scope.libri.find(x => x.titolo === $scope.titolo)在推送到陣列之前在您的 add 函式中使用。
.find() 來自 MDN
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/371069.html
標籤:angularjs
下一篇:在某些條件下隱藏輸入欄位
