【问题标题】:Apply ng-if in ng-repeat only to selected item仅在 ng-repeat 中将 ng-if 应用于所选项目
【发布时间】:2015-11-17 01:00:21
【问题描述】:

我正在开发一个使用 Ionic 框架的跨浏览器应用程序。我设置了主从模式页面。我的详细信息页面上有三个项目。当我删除其中一项时,我想使用 ng-if 指令从 DOM 中删除一个 message-div,但前提是用户单击了 ionicPopup 确认屏幕中的“是”按钮。目前我可以更改所有三个项目,但我很难弄清楚如何仅将更改附加到所选项目。代码如下:

Detail.html:

<ion-view title="Events" cache-view="false">
<ion-content has-header="true" padding="false">
    <div class="list card" ng-repeat="details in tag.Activity track by $index">
        <div ng-if="!deletedItem">
            This item will be deleted.
        </div>
        <div class="item item-divider item-stable" ng-click="toggleGroup(details)" ng-class="{ active: isGroupShow(details) }">
            <i class="icon" ng-class="isGroupShown(details) ? 'ion-arrow-down-b' : 'ion-arrow-right-b'"></i>
            &nbsp;
            {{ details.ActivityName }}
        </div>
        <div class="item-body item-accordion" ng-show="isGroupShown(details)">
            <label class="item">
                <span class="input-label item-stacked-label">Hours</span>
                <input type="hours" value="{{ details.Hours}}">
            </label>
        </div>

        <div class="item tabs tabs-secondary tabs-icon-left">
            <a class="tab-item" ng-click="deleteHours($index, details.RecordID)">
                <i class="icon ion-close-circled"></i>
                Delete Hours
            </a>
        </div>

    </div>
</ion-content>

Controller.js:

$scope.deleteHours = function(column,id){
    $ionicPopup.confirm({
        title: "<b>Delete Hours</b>",
        template: "Are you sure you want to delete these hours?",
        buttons: [
            { text: 'Cancel', onTap: function(e) { return true; } },
            {
                text: "<b>I'm sure</b>",
                type: 'button-positive',
                onTap: function(e) {
                    $scope.deletedItem = true;
                    console.log("Column: " + column);
                }
            }
        ]
    })

目前,列号打印正确。正在传递值。我只是无法弄清楚将列号正确附加到 ng-if 的语法。任何帮助将不胜感激。

【问题讨论】:

  • $scope.deletedItems = []; $scope.deletedItems.push(column); ng-if="deletedItems.indexOf($index)==-1"
  • 谢谢!这对我也有很大帮助!!!

标签: angularjs ng-repeat angular-ng-if


【解决方案1】:

您将列传递给deleteHours 函数,为什么不将pendingDeletion 属性附加到详细信息?

<ion-view title="Events" cache-view="false">
<ion-content has-header="true" padding="false">
    <div class="list card" ng-repeat="details in tag.Activity track by $index">
        <div ng-if="details.pendingDeletion">
            This item will be deleted.
        </div>
        <div class="item item-divider item-stable" ng-click="toggleGroup(details)" ng-class="{ active: isGroupShow(details) }">
            <i class="icon" ng-class="isGroupShown(details) ? 'ion-arrow-down-b' : 'ion-arrow-right-b'"></i>
            &nbsp;
            {{ details.ActivityName }}
        </div>
        <div class="item-body item-accordion" ng-show="isGroupShown(details)">
            <label class="item">
                <span class="input-label item-stacked-label">Hours</span>
                <input type="hours" value="{{ details.Hours}}">
            </label>
        </div>

        <div class="item tabs tabs-secondary tabs-icon-left">
            <a class="tab-item" ng-click="deleteHours($index, details)">
                <i class="icon ion-close-circled"></i>
                Delete Hours
            </a>
        </div>

    </div>
</ion-content>

controller.js

$scope.deleteHours = function(column,detail){
    //pass detail, not record id. you can change this if you want to do information hiding.
    $ionicPopup.confirm({
        title: "<b>Delete Hours</b>",
        template: "Are you sure you want to delete these hours?",
        buttons: [
            { text: 'Cancel', onTap: function(e) { return true; } },
            {
                text: "<b>I'm sure</b>",
                type: 'button-positive',
                onTap: function(e) {
                    detail.pendingDeletion = true; //attach to detail
                    console.log("Column: " + column);
                }
            }
        ]
    })

【讨论】:

  • 效果很好!太感谢了。回想起来,我可以看到我在很大程度上过度考虑了这个问题。再次感谢!!!
  • 没问题,当你遵循这个模式时,只是一个警告,如果你稍后保存(在这种情况下你正在删除)这个实体到后端,请记住你已经装饰了这个detail 具有新属性,因此请确保在将其序列化到后端之前对其进行清理。一些后端在看到非类成员的额外属性时可能会感到恐慌。
  • 嘿 maschwenk - 只是想让您知道,我已经多次阅读您的评论,我想我明白您的意思,我非常感谢您对这个潜在问题的提醒。了解潜在的绊脚石总是好的!再次感谢你! :)
猜你喜欢
  • 2015-09-13
  • 1970-01-01
  • 2016-08-28
  • 2023-03-04
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多