2016-11-11 50 views
8

是的,它之前已經被問過了,我已經閱讀了所有的答案,但似乎沒有任何工作。所以我要求額外的一雙眼睛,看看你是否能夠在我的代碼中發現任何奇點,使它無法正常工作。 (我試過這個代碼和邏輯的其他地方,它似乎工作正常)。在控制檯中沒有錯誤的方式使用Angular從陣列中刪除項目

我只是試圖從某個視圖中刪除一個項目,當有人點擊圖片上的x。

這裏是控制器

app.controller('galleryController', ['$scope', '$http', function($scope, $http) { 
    $http.get('data/galleries.json').success(function(data){ 
     $scope.galleries = data; 
    }).error(function(error) { 
     console.log(error); 
    }); 

    $scope.removeGalleryItem=function(gallery){ 
     var removedGallery = $scope.galleries.indexOf(gallery); 
     $scope.galleries.splice(removedGallery, 1); 
    }; 
}]); 

和我的觀點

<div class="col-xs-12 col-md-3" ng-repeat="gallery in galleries" > 
    <a class="gallery-item" ng-href="{{gallery.img}}" ng-class="{true:'active',false:''}[checked]" 
     title="Nature Image 1" > 
     <div class="image"> 
      <img ng-src="{{gallery.img}}" alt="Nature Image 1"/> 

     </div> 
     <div class="meta"> 
      <strong>{{gallery.title}}</strong> 
      <span>{{gallery.desc}}</span> 
     </div> 
    </a> 
    <ul class="gallery-item-controls"> 
     <li><label class="check"><input type="checkbox" class="icheckbox" ng-model="checked" /></label></li> 
     <li><span class="gallery-item-remove"><i class="fa fa-times" ng-click="removeGalleryItem(gallery)"></i></span></li> 
    </ul> 
</div> 

角1.5.8

感謝

+0

你的意思是你想從DOM和Array中摧毀那個特定的對象? – AndreaM16

+0

好問題。只是從DOM。這是爲了嘲笑的目的。向客戶展示當全部開發時會發生什麼。那時它會被從數據庫中刪除,但不是現在 – LOTUSMS

+0

檢查我的答案。我使用'Lodash'作爲對象和數組。它很容易擺脫這樣的事情。 Lodash:https://lodash.com/docs/4.16.6 – AndreaM16

回答

12

您可以像這樣在您的點擊功能中傳遞$index

<i class="fa fa-times" ng-click="removeGalleryItem(galleryItem, $event , $index)"> 

,並使用$scope.galleries.splice(index, 1);您點擊功能removeGalleryItem內,請確保您有索引參數太喜歡這一點。

$scope.removeGalleryItem = function(gallery , event, index){ 
     $scope.galleries.splice(index, 1); 
    }; 

希望這有助於..

+0

只要我在控制器中有數據,但不能在數據存在於單獨的文件中時使用,而且我使用$ http遠程使用它。所以,你的解決方案的工作原理,我將它標記爲一個解決方案,但任何想法,爲什麼它不按預期工作?這是一個codepen,顯示你的解決方案的作品,但如果你註釋掉數據並且把$ http連接留給數據文件,它就會失敗http://codepen.io/LOTUSMS/pen/eBzNOr – LOTUSMS

+0

@LOTUSMS我用Chrome測試了這個Pen,從控制檯中可以看出,由於Cross-Origin問題,Firefox和數據無法恢復。 –

+0

@camden_kid謝謝你的關注。我將網站推入服務器,以便它們都位於相同的域和原點內。 http://joli.sitedev.online/#/gallery但它仍然無法正常工作。還是我誤解了交叉來源是什麼? – LOTUSMS

1

如果我理解正確你的問題,如果你想刪除來自DOM和包含這些特定元素的Array的特定元素,您可以執行此操作如下:

<!-- Intercept that particular Element with $event--> 
<i class="fa fa-times" ng-click="removeGalleryItem(galleryItem, $event)"> 

讓我們假設你正在重複一些galleryItems並且它們有一個name屬性。

而且您的控制器上:

$scope.removeGalleryItem(galleryItem, $event){ 
    //Save galleryItem Name 
    var itemName = $($event.currentTarget).name(); // if it has it on html 
    var itemName = galleryItem.name; // if it has a property 
    //Get the target and remove it 
    $($event.currentTarget).remove(); 

    //Using lodash, loop through your array and remove that exact object from it. 
    //Ofc you can do a normal loop through it. 
    $scope.galleries = _.remove($scope.galleries, function(n) { 
     return n != itemName; 
    }); 

    //Then, if the change does not happen in your DOM 
    $scope.$apply(); 

} 

希望我一直有幫助。

+0

不幸的是,什麼都沒發生 – LOTUSMS

+0

嘗試:'console.log($($ event.currentTarget));'並告訴我結果。 – AndreaM16

+0

嗯,它會拋出一個錯誤,但當我將它添加到函數中時它就會消失。我會再次運行它 – LOTUSMS

4

之後做一些研究,我認爲這個問題是galleryController某處定義在您的標記,但在畫廊中的元素是不是如該控制器裏面定義。

參照http://joli.sitedev.online/#/gallery。在文件slidesController。JS那裏galleryController定義我把一個破發在這裏和代碼暫停:

enter image description here

我也把一個破發點這裏,但在一個刪除按鈕,當點擊該代碼不會暫停:

enter image description here

望着標記我看不到任何ng-controller="galleryController"跡象,所以我不能看到畫廊ng-repeat如何填充:

enter image description here

也許是通過這個:

enter image description here

如果通過那就說明事情作爲指令有其自己的控制器。任何進一步的信息將有所幫助,我相信我們可以清除這一點。

1

我已對修復this問題做了一些更改,您可以在this鏈接查看它。

這裏的問題是在調用removeGalleryItem(galleryItem, $event)的html代碼片段中存在拼寫錯誤。參數名稱應該是gallery而不是galleryItem,因爲名稱galleryItem中沒有這樣的對象,因此在此方法中,參數值爲undefined。一旦我固定它,我能得到removeGalleryItem方法中的畫廊對象,下面的代碼工作絕對沒問題:

$scope.removeGalleryItem=function(gallery){ 
    var selectedGallery = $scope.galleries.indexOf(gallery); 
    $scope.galleries.splice(selectedGallery, 1); 
}; 

還要注意的是,我已刪除從方法聲明中,並從該$事件屬性html方法調用,因爲我們在上面提到的方法中並不需要它。

<i class="fa fa-times" ng-click="removeGalleryItem(gallery)"></i>