【发布时间】:2019-04-14 18:35:08
【问题描述】:
如何清除具有选定值的数组,以便值可以返回到选择中?
我有一个人员数组。人员数组值在选择中可用。当我选择名称时,它们将被转移到multipleDemo 数组。而且您不能从 select 中重新选择它们,因为它们会消失并移动到 multipleDemo 数组中。使用 Delete 按钮,我必须将 multipleDemo 数组中的所有元素(第一个元素除外)删除到 people 数组中。这样您就可以再次从选择中选择一个名称。函数$clearTag 出错。
预期行为: 示例:
- 选择:弗拉基米尔
- 出现标签弗拉基米尔
- 选择弗拉基米尔(您不能选择弗拉基米尔,因为他已经被选择)
- 单击删除。用 multipleDemo 数组剪切元素(标签)并将它们放入数组 people 中
- 您可以再次选择 Wladimir
这是我的代码:http://plnkr.co/edit/TPZjXkkSRrIc5ApzP07F?p=preview
index.html
<!DOCTYPE html>
<html lang="en" ng-app="demo">
<head>
<meta charset="utf-8">
<title>AngularJS ui-select</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular-sanitize.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css">
<!-- ui-select files -->
<script src="select.js"></script>
<link rel="stylesheet" href="select.css">
<script src="demo.js"></script>
<!-- Select2 theme -->
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.4.5/select2.css">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.8.5/css/selectize.default.css">
<style>
body {
padding: 15px;
}
.select2 > .select2-choice.ui-select-match {
/* Because of the inclusion of Bootstrap */
height: 29px;
}
.selectize-control > .selectize-dropdown {
top: 36px;
}
</style>
</head>
<body ng-controller="DemoCtrl">
<h3>Array of strings</h3>
<button ng-click='clearTag()'>Delete</button>
<ui-select tagging tagging-label="new tag" multiple ng-model="multipleDemo"
on-select="OnClickSelect($item)" on-remove="OnRemoveSelect($item)"
theme="select2" ng-disabled="disabled" style="width: 300px;">
<ui-select-match placeholder="Select name...">{{$item.name}}</ui-select-match>
<ui-select-choices repeat="item in people | filter:$select.search">
{{item.name}}
</ui-select-choices>
</ui-select>
<p>Selected: {{multipleDemo}}</p>
<hr>
</body>
</html>
demo.js
app.controller('DemoCtrl', function($scope, $http, $timeout) {
$scope.multipleDemo =[];
$scope.people = [
{ name: 'Adam', email: 'adam@email.com', age: 12, country: 'United States' },
{ name: 'Amalie', email: 'amalie@email.com', age: 12, country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania@email.com', age: 21, country: 'Argentina' },
{ name: 'Adrian', email: 'adrian@email.com', age: 21, country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir@email.com', age: 30, country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha@email.com', age: 30, country: 'United States' },
{ name: 'Nicole', email: 'nicole@email.com', age: 43, country: 'Colombia' },
{ name: 'Natasha', email: 'natasha@email.com', age: 54, country: 'Ecuador' },
{ name: 'Michael', email: 'michael@email.com', age: 15, country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas@email.com', age: 43, country: 'Colombia' }
];
$scope.OnClickSelect=function(item)
{
$scope.multipleDemo.push(item.name);
}
$scope.OnRemoveSelect = function(item) {
var index = $scope.people.indexOf(item.name);
$scope.people.splice(index, 1);
}
$scope.clearTag = function() {
for(var i =0; i < $scope.multipleDemo.length; i++) {
$scope.multipleDemo.splice($scope.multipleDemo[i], 1000);
$scope.people.push($scope.multipleDemo[i]);
}
}
【问题讨论】:
-
我不明白您所期望的行为。顺便说一句,您的 clearTag 函数不会被调用,因为您在 js 中将其声明为 $scope 但在 html 中将其称为 $ctrl跨度>
-
功能清除标签。使用拼接方法切出想要放回人员数组中的元素
-
您应该阅读
ngController指令的工作原理。您可以将$scope注入您的控制器,如您所展示的那样,并在没有对象引用的情况下访问范围变量,或者您使用ng-controller="controller as $ctrl"之类的东西并使用$ctrl.clearTag()语法。您目前正在尝试同时使用两者。您的控制台中可能对此有错误;如果您需要进一步的帮助,请在您的问题中显示。 -
我删除了$ctrl。我有 ng-click="clearTag()"。我在控制台中没有任何错误。
标签: javascript arrays angularjs angular-ui-select