javascript - 删除动态添加到 ng-repeat(angularjs) 的值

标签 javascript jquery angularjs validation angularjs-ng-repeat

首先祝大家感恩节快乐!!

我有这个笨蛋-> http://plnkr.co/edit/N2bs5xqmtsj3MmZKEI25?p=info 用户选择所有三个值,然后才启用“添加”按钮进行添加。

单击后,所选条目将使用 ng-repeat 显示在下面。我还在添加的每一行上添加了一个删除按钮。在这种情况下,我如何确保删除功能正常工作。 ?即,如果我单击“删除”,则只会删除该特定行。

其次,如果您在第一次添加期间注意到,第一行上方会显示一个删除按钮。我怎样才能删除它?

我还想将选定的文件保存在 Controller 中,以便我可以将这些数据提供给后端。我如何知道用户选择了哪些选项。

这是 plunker 代码- HTML

<div ng-controller="Controller">
<form name="form" novalidate>
     <input type='file' onchange="angular.element(this).scope().fileNameChanged(this)" ng-model="document" valid-file required>
               <select name="singleSelect" ng-model="activeItem.content" ng-options="foo as foo for foo in contentarray" required>
                   <option ng-if="contentarray.length > 1" value="" selected>Choose</option>
               </select>

               <select name="singleSelect1" ng-model="activeItem.content1" ng-options="foo as foo for foo in content1array" required>
                   <option ng-if="content1array.length > 1" value="" selected>Choose</option>
               </select>
               <button ng-click="addItem()" ng-disabled="disableAdd || (form.$invalid && (!form.$valid && 'invalid' || 'valid'))">Add</button>
</form>

<div ng-repeat="item in items" ng-show="isvisible">
      <a>{{item.name}}</a>
      <a>{{item.content}}</a>
      <a>{{item.content1}}</a>
      <button ng-click="deleteItem()">Delete</button>
</div>
</div>

JS代码

var app = angular.module('Select', []);

app.controller('Controller', function($scope, $timeout) {
/* for adding optional file based selection values selected by the user*/
$scope.isvisible = false;  /* display 1st line  when user clicks on add button*/
$scope.items = [{
}];

$scope.activeItem = {
    name: '',
    content: '',
    content1: ''
}
$scope.fileNameChanged = function (el) {
    $scope.activeItem.name = el.files[0].name
}


$scope.addItem = function () {
    $scope.isvisible = true;
    $scope.items.push($scope.activeItem);
    if ($scope.items.length > 6) {
        $scope.disableAdd = true
    }
    $scope.activeItem = {} /* reset active item*/
    angular.element("input[type='file']").val(null); /* To clear the input type file selected for next selection*/
}




/* for showing select options and enabling add button only when both the options are selected*/
$scope.content = {};
$scope.content1 = {};

$scope.contentarray = ['2', '3'];
$scope.content1array = ['12', '121', '1233', '1211'];

$scope.trigger = function () {
    $timeout(function () {
        $('form.bad select').trigger('change');
    })
}

});

最佳答案

我指的是您的deleteItem()delete-button问题:

您必须在 Controller 中实现 deleteItem() 方法。当您从模型 ìtems 中删除项目时,ng-repeat 将自动更新您的列表,无需 angular.element。由于 Angular 中的双向数据绑定(bind),这是可行的。

向您的ìtems添加一个ID,并使用它从模型中删除该项目,例如:

$scope.addItem = function() {
    //...
    $scope.activeItem.id = $scope.items.length;
    $scope.items.push($scope.activeItem);
    //...
}

$scope.deleteItem = function(item) {
    $scope.items.splice(item.id, 1);
}

在您的ng-repeat中,您需要像这样定义按钮:

<button ng-click="deleteItem(item)">Delete</button>

您的另一个问题是附加的删除按钮:该按钮会显示出来,因为您使用空元素初始化模型ìtems。像这样初始化,按钮将不会显示:

$scope.items = [];

希望有帮助

关于javascript - 删除动态添加到 ng-repeat(angularjs) 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33924653/

相关文章:

javascript - 如何创建具有重叠 View 的轮播? [包含 jsFiddle 和 ASCII 艺术}

javascript - 提交时弹出对话框

jquery - 检查div是否设置了背景图片

javascript - 使用 jQuery 过滤元素

javascript - js 销毁子对象内部对象

javascript - 为什么模型和 Controller 的概念混合在 Angular.JS 中?

javascript - Facebook 分享社交插件的 javascript 代码错误

angularjs - AngularJS 中不同形式的 scope.$apply 有什么作用?

angularjs - 使用 Restangular 查询 Node api/MongoDB

css - ionic 可点击元素图标