如何从列表中删除该项目

时间:2016-11-08 09:34:48

标签: angularjs ionic-framework

我使用ionic和angularjs创建一个应用程序。在应用程序开发中,我使用离子的滑动选项从主列表中删除特定的优惠券。我在这里面临的问题是,我只能删除所选优惠券的描述,但不能从主列表中删除/删除优惠券。我也使用了$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());
    }

2 个答案:

答案 0 :(得分:1)

如一个答案所示,您可以使用$index机制从数组中删除对象。

您必须同时删除items arrayrootscope array

查看:

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

<强>控制器:

 $scope.deleteSelected = function (index,coupons) {
     $rootScope.couponList.splice(index,1);

     var to_delete = $scope.items.find(find_data)

     function find_data(items) { 
        return items.CouponTitle === coupons.CouponTitle;
     }

     var index = $scope.items.indexOf(to_delete);

     $scope.items.splice(index,1)


 }

HEre is a partial implemented fiddle

您应该同时删除items数组,并couponList删除ng-repeat

答案 1 :(得分:0)

将deleteSelected内的索引作为参数传递,并直接从数组中删除。

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

但是对于$ index,您必须使用ng-repeat的$ index机制跟踪。

如果您想了解更多信息,请与我们联系。