【发布时间】:2016-02-28 17:22:18
【问题描述】:
首先祝大家感恩节快乐!!
我有这个 plunker-> http://plnkr.co/edit/N2bs5xqmtsj3MmZKEI25?p=info 用户选择所有三个值,然后才启用“添加”按钮进行添加。
单击后,使用 ng-repeat 将显示以下所选条目。我还在每一行添加了一个删除按钮按钮。在这种情况下,我如何确保删除功能有效。 ?即,如果我单击删除,则只会删除该特定行。
其次,如果您在第一次添加时注意到了,则在第一行上方会显示一个删除按钮。我怎样才能删除它?
我还想将选定的文件保存在控制器中,以便将这些数据提供给后端。我如何知道用户选择了哪些选项。
这是 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');
})
}
});
【问题讨论】:
标签: javascript jquery angularjs validation angularjs-ng-repeat