2016-05-06 70 views
-1

如何檢查添加的單詞是否已存在,無論大小寫? 檢查是否區分大小寫,然後如果是,那麼它已經存在?Angular JS如何檢查添加的單詞是否已存在

app.controller("myCtrl", function($scope) { 
    $scope.products = ["BMW", "AUDI", "BENTLY"]; 
    $scope.addItem = function() { 
     $scope.errortext = ""; 
     if (!$scope.addMe) {return;} 


     if ($scope.products.lastIndexOf($scope.addMe) == -1) 
     { 

      $scope.products.push($scope.addMe); 
     } 
     else { 
      $scope.errortext = "The item is already in your shopping list."; 
     } 
    } 
    $scope.removeItem = function (x) { 
     $scope.errortext = "";  
     $scope.products.splice(x, 1); 
    } 

回答

0

功能你需要的是:

function isAddedToList(item) { 
    $scope.products.forEach(function(product){ 
     if(item === product){ 
      return true; 
     } 
    }); 
} 
相關問題