2016-06-28 57 views
3

爲什麼我無法從陣列帖子中刪除項目?對於刪除項如何從數組angularjs中刪除項目?

html tag 
<span ng-click="remove($index)"> delete</span> 

//AngularJS method where I try to delete item 
blog.remove = function(index) { 
blog.posts.splice(index, 1); 
}; 

//Angular array posts 
blog.posts = [{ 
    "title": "Blog Post One", 
    "comments": [ 
     { 
     "body":"Lorem ipsum dolor sit amet, consectetur adipisicing elit. ", 
     "author": "trollguy87" 
     } 
    ]}]; 

HTML標籤在哪裏可以是問題嗎?

+0

一種可能性是你逝去在刪除函數索引是錯誤的。嘗試在該拼接語句之前通過console.log查看索引。 – Yogesh

+1

也許發佈了更多的代碼。很難通過查看這些片段來確切地發現問題。 在問題上我看到'remove'正在調用範圍。但你的定義是在博客本身。那麼,它不應該是'blog.remove($ index)'? 而且,$ index是什麼?來自某種'重複'我假設 – Chanthu

+1

請張貼包含'ng-repeat'部分的代碼,目前很難判斷您是如何管理'$ scope/vm' – Icycool

回答

0

如果您正在使用ng-repeat那麼這可以幫助:

<div ng-repeat="key in posts"> <!-- This will use your blog.posts --> 
    <button ng-click="posts.splice($index, 1)"> 

     {{key.title}} 
    </button> 
</div>