我是新的Angular-js請用編輯動態表格創建.I添加一些記錄幫助Json Ang ng-model但如何刪除當前記錄點擊刪除按鈕。 與此運算符一起傳遞。如何刪除當前記錄當點擊刪除按鈕
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
\t $scope.emplist = [
\t {empname:'samudrala',empsalary:'4.5 - pam',empid:'Emp - 450'},
\t {empname:'soujanya',empsalary:'4.5 - pam',empid:'Emp - 451'},
\t {empname:'suguna',empsalary:'4.5 - pam',empid:'Emp - 452'},
\t {empname:'sangeetha',empsalary:'4.5 - pam',empid:'Emp - 453'},
\t {empname:'sadhanandham',empsalary:'4.5 - pam',empid:'Emp - 454'},
\t {empname:'jai',empsalary:'4.5 - pam',empid:'Emp - 455'},
\t {empname:'vijay',empsalary:'4.5 - pam',empid:'Emp - 456'},
\t {empname:'Ajay',empsalary:'4.5 - pam',empid:'Emp - 457'},
\t {empname:'Sandya',empsalary:'4.5 - pam',empid:'Emp - 458'},
\t {empname:'Raamu',empsalary:'4.5 - pam',empid:'Emp - 459'}
\t ];
\t $scope.addItem = function(){
\t \t $scope.emplist.push({'empname':$scope.empname,'empsalary':$scope.empsalary,'empid':$scope.empid});
\t \t $scope.empname = '';
\t \t $scope.empsalary = '';
\t \t $scope.empid = '';
\t }
\t $scope.remItem = function(x){
\t \t $scope.emplist.splice(x,1);
\t }
});
body{
\t font-size: 14px;
\t font-family: Arial;
\t color:#333;
}
<!DOCTYPE Html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<table style="width:100%;">
<tr height="25" style=" background: #99ff00;">
<th width="5%"></th>
<th width="40%">EMP Name</th>
<th width="30%">EMP Salary</th>
<th width="25%">EMP ID</th>
</tr>
<tr height="25" ng-repeat="x in emplist">
<td style="text-align: center; background: #99ff00;" ng-if="$odd" >{{$index}}</td>
<td style="text-align: center; background: #00ff00;" ng-if="$even" >{{$index}}</td>
<td style="text-align: center; background: #99ff00;" ng-if="$odd" >{{x.empname}}</td>
<td style="text-align: center; background: #00ff00;" ng-if="$even" >{{x.empname}}</td>
<td style="text-align: center; background: #99ff00;" ng-if="$odd">{{x.empsalary}}</td>
<td style="text-align: center; background: #00ff00;" ng-if="$even">{{x.empsalary}}</td>
<td style="text-align: center; background: #99ff00;" ng-if="$odd">{{x.empid}} <button ng-click="remItem();" style="background:#00ffff; border:0px;">× Remove</button></td>
<td style="text-align: center; background: #00ff00;" ng-if="$even">{{x.empid}} <button ng-click="remItem();" style="background:#00ffff; border:0px;">× Remove</button></td>
</tr>
<tr height="25">
<td><button ng-click="addItem();" style="background: #00ffff; border:0px; width:100%; height:100%;">Add</button></td>
<td style="padding:2px;"><input type="text" ng-model="empname" style="width:100%;" ></td>
<td style="padding:2px;"><input type="text" ng-model="empsalary" style="width:100%;" ></td>
<td style="padding:2px;"><input type="text" ng-model="empid" style="width:100%;" ></td>
</tr>
</table>
</div>
</body>
</html>
在ng-repeat指令中,特殊屬性公開本地作用域包括「$ index」,所以你不需要在remove函數中搜索indexOf項: – etiennecrb
如果在'ng-repeat'中使用過濾器,則不能使用'$ index'。 –