2016-10-06 77 views
0

我想從輸入字段中刪除數據,但它不會刪除。無法從angularjs的輸入字段中刪除對象

var myApp = angular.module('myApp',[]); 
 

 
myApp.controller('MyCtrl', function MyCtrl($scope) { 
 
    $scope.items = [ 
 
     { 
 
      name: 'item 1', 
 
      id: '4' 
 
     }, 
 
     { 
 
      name: 'item 2', 
 
      id: '3' 
 
     }, 
 
     { 
 
      name: 'item 3', 
 
      id: '2' 
 
     }, 
 
     { 
 
      name: 'item 4', 
 
      id: '1' 
 
     } 
 
    ]; 
 
    
 
    $scope.delete = function (item) { 
 
     $scope.items.splice($scope.items.indexOf(item), 1); 
 
    } 
 
});
<div ng-controller="MyCtrl"> 
 
    <div ng-repeat="item in items | orderBy: 'id'" ng-click="delete(item)"> 
 
     <span> 
 
      Hello, {{item.name}}! 
 
     </span> 
 
    </div> 
 
    <input type="text" ng-model="items" /> 
 
</div> 
 
<script src="https://code.angularjs.org/1.4.0/angular.js"></script>

這裏是JSFIDDLE

$scope.delete = function (item) { 
     $scope.items.splice($scope.items.indexOf(item), 1); 
    } 
+0

''爲什麼你陣列'items'作爲'NG-model'? – taguenizy

+0

bcos我必須通過這個數組來服務 – Kranthi

+0

你真的想要什麼?當你輸入'item'的名字並且點擊一個按鈕時說'delete'。那麼想從數組中刪除特定的項目? –

回答

0

修改了你的用戶界面。

增加了一個按鈕那裏選擇item

delete只給該項目的名稱輸入字段

var myApp = angular.module('myApp',[]); 
 

 
myApp.controller('MyCtrl', function MyCtrl($scope) { 
 
    $scope.items = [ 
 
     { 
 
      name: 'item 1', 
 
      id: '4' 
 
     }, 
 
     { 
 
      name: 'item 2', 
 
      id: '3' 
 
     }, 
 
     { 
 
      name: 'item 3', 
 
      id: '2' 
 
     }, 
 
     { 
 
      name: 'item 4', 
 
      id: '1' 
 
     } 
 
    ]; 
 
    
 
    $scope.delete = function (item) { 
 
    \t for(var x in $scope.items){ 
 
     \t if($scope.items[x].name == item){ 
 
     \t $scope.items.splice($scope.items.indexOf($scope.items[x]), 1); 
 
      $scope.toRemove = '' // clear input field after deleting 
 
      } 
 
     } 
 
    } 
 
});
<div ng-app="myApp"> 
 
    <div ng-controller="MyCtrl"> 
 
    <div ng-repeat="item in items | orderBy: 'id'"> 
 
     <span> 
 
      Hello, {{item.name}}! 
 
     </span> 
 
    </div> 
 
    <input type="text" ng-model="toRemove" /> 
 
    <button ng-click="delete(toRemove)">delete</button> 
 
    </div> 
 
</div> 
 
<script src="https://code.angularjs.org/1.4.0/angular.js"></script>

這裏要被刪除的工作demo

+0

@Kranthi更新了答案。現在你可以檢查它在上面給出的代碼片段上工作 –

0

問題與您的代碼是

$scope.delete = function (item) { 
    $scope.items.splice($scope.items.indexOf(item), 1); 
} 
$scope.items.indexOf(item) -- You cannot fetch index of an "object" in an array by directly using "indexOf(item)". 

試試這個

$scope.delete = function (item) { 
     // fetch the position of item in items array 
     position = $scope.items.map(function(e) { return e.name; }).indexOf(item.name); 
     $scope.items.splice(position, 1); 
     console.log($scope.items); 
    } 

Plunker

0

試試這個,也許這符合您的要求。

var app = angular.module('myApp', []); 
 
\t app.controller('myCtrl', function($scope) { 
 
\t  $scope.items = [ 
 
\t      {name : 'item1', id : '4'}, 
 
\t      {name : 'item2', id : '3'}, 
 
\t      {name : 'item3', id : '2'}, 
 
\t      {name : 'item4', id : '1'} 
 
\t      ]; 
 
\t  $scope.addTextField = function(name){ 
 
\t  \t $scope.textFiles = name; 
 
\t  }; 
 
\t  $scope.remove = function(item){ 
 
\t  \t for (var i=0; i<$scope.items.length; i++){ 
 
\t  \t \t if($scope.items[i].name == item){ 
 
\t  \t \t \t $scope.items.splice($scope.items.indexOf($scope.items[i]), 1); 
 
\t  \t \t \t $scope.textFiles = ''; 
 
\t  \t \t } 
 
\t  \t } 
 
\t  }; 
 
\t });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCtrl" > 
 
\t \t <div ng-repeat="item in items | orderBy: 'id'" ng-click="addTextField(item.name);"> 
 
\t \t \t Click me {{item.name}}! 
 
\t \t </div> 
 
\t \t <input type="text" ng-model="textFiles"><input type="button" value="X" ng-click="remove(textFiles);"> 
 
\t </div>