2017-05-01 97 views
0

我是AngularJS的新手,我需要一些幫助來嘗試刪除按鈕,以刪除AngularJS中的用戶。如果有人能幫助,我會感激的。提前致謝。如何使刪除按鈕在AngularJS中刪除用戶?

這裏是我的刪除按鈕,我創建

<button type="button" 
     class="delete" 
      ng-click="" 
      ng-show="!inactive">Delete</button> 
    //The user view 
    <div class="people-view"> 
    <h2 class="name">{{people.first}}</h2> 
    <h2 class="name">{{people.last}}</h2> 

    <span class="title">{{people.title}}</span> 

    <span class="date">{{people.date}} </span> 



</div> 



//Controller 
app.controller('MainController',['$scope', 'people', '$routeParams', 
function($scope, people, $routeParams) { 
people.success(function(data) { 
$scope.people = data[$routeParams.id]; 


    }); 
    }]); 
+1

其中是您的用戶的邏輯或細節。請發帖 –

+0

查看更新後的問題請 –

+0

你用ng-repeat來顯示用戶嗎?你在服務器端使用什麼語言? – Akashii

回答

0

您必須填寫ng-click行動,你的函數在控制器中。 如果你的按鈕在ng-repeat也許你必須發送參數在你刪除功能。

2

因此在ng-click中調用一個名爲deleteUser的函數並傳遞給該用戶的id。

//HTML 
<button type="button" 
     class="delete" 
      ng-click="deletePeople(id)" 
      ng-show="!inactive">Delete</button> 
//The user view 
<div class="people-view"> 
    <h2 class="name">{{people.first}}</h2> 
    <h2 class="name">{{people.last}}</h2> 
    <span class="title">{{people.title}}</span> 
    <span class="date">{{people.date}} </span> 
</div> 

所以現在你的控制器裏面有一個函數來刪除用戶。如果你在ng-repeat裏使用它,那麼你應該相應地傳遞參數。根據你的代碼,你可以通過它。

//Controller 
$scope.deletePeople = function (id){ 
    //here comes your delete code. 
    //pass id from the ng-click function. 
    //based on that id find the record in the $scope.people array and remove that from the array. 
    //you can use javascript splice function to remove it. 

    var userToDelete; 
    $scope.people.forEach(function(p, index){ 
    if(p.id == id){ 
     userToDelete = index; 
    } 
    }); 

    $scope.people.splice(userToDelete, 1); 
} 

這裏是一個蹲下的例子。 https://plnkr.co/edit/GbKHSER1r992D5ImXJ7n?p=preview

+0

所以對於我的deletePeople函數(id),我可以這樣做:var index = $ scope.people.indexOf(people); $ scope.people.splice($ index,1); –

+0

Yaa肯定的是,如果您使用的是ng-repeat,那麼您可以在ng-click函數中傳遞索引,並且基於該索引您可以刪除特定的人或以這種方式也可以執行相同的操作。 –

+0

如果我不使用ng-repeat,該怎麼辦?你能告訴我你實現它的功能嗎? –