2016-11-08 168 views
1

我使用ionic和angularjs創建應用程序。在應用程序開發過程中,我使用離子的swipe選項從主列表中刪除特定的優惠券。我在這裏面臨的問題是,Im只能刪除所選優惠券的說明,但無法從主列表中刪除/刪除優惠券。我還用$rootScope在那裏宣佈我的json array。我已經申報了一個$scope.item,其中所選優惠券及其詳細信息將被推入,以便顯示每個選定優惠券的說明。我在代碼的某個地方出錯了,請幫助我解決問題。謝謝。如何從列表中刪除項目

HTML:

<ion-list> 
     <ion-item ng-click="select_item(coupons)" ng-repeat="coupons in couponList" ng-model="coupons.selected"> 
      {{coupons.CouponTitle}} <br> 
      <ion-option-button ng-click="editCoupons(coupons)">Edit</ion-option-button> 
      <ion-option-button class="button-assertive" ng-click="deleteSelected(coupons)">Delete</ion-option-button> 
     </ion-item> 
    </ion-list> 
    <hr> 
    <div style="text-align:center"> 
     <div ng-repeat="item in items"> 
      Coupon offer: {{item.data.description}}<br> Valid From: {{item.data.Fromldate}} 
      <br> Valid Till: {{item.data.Todate}} </div> 

控制器:

$scope.items = []; 
     $rootScope.couponList = [{ CouponTitle: "Purchase worth $100", data: {description: "$50 off", Fromldate: "2016-09-09", Todate: "2016-09-18"}}, 
     {CouponTitle: "Purchase worth $300", data:{description: "$75 off", Fromldate: "2016-11-09", Todate: "2016-10-19"}}, 
     { CouponTitle: "Purchase worth $500",data:{description: "$95 off", Fromldate: "2016-09-10", Todate: "2016-09-30"}}]; 

     $scope.select_item = function (key) { 
     $scope.items.push(key); 

     } 

$scope.deleteSelected = function() { 
      $scope.items.splice($scope.items.indexOf()); 
    } 

回答

1

正如在一個答案中所建議的,您可以使用$index機制從數組中刪除一個對象。

你必須同時刪除從items arrayrootscope array

查看:

<ion-option-button class="button-assertive" ng-click="deleteSelected($index,coupons)">Delete</ion-option-button> 

控制器:

​​

HEre is a partial implemented fiddle

您應該刪除這兩個項目陣列也,couponList去除ng-repeat

+0

@jazzoria,請檢查我的答案。 – Sravan

+0

你能解釋一下如何使用'ng-repeat'來解決這個問題。請 – jazzoria

+0

這是一樣的,你檢查,檢查小提琴鏈接, 'ng-model =「coupons.selected」>「ng-model =」coupons.selected「>」 Sravan

0

內部deleteSelected通索引作爲參數,並從陣列直接刪除。

<ion-option-button class="button-assertive" ng-click="deleteSelected($index)">Delete</ion-option-button> 

但是$索引你將不得不NG重複的$索引機制使用的軌道。

讓我知道你是否想要更清晰。

+0

我用$指標,但能夠只刪除主從列表中的描述,但沒有優惠券。 @ Jigar7521 – jazzoria