I need to assign a color to a person. I have two arrays: people and colors. 1.When I choose Adam, it should be pink, Estefania - yellow, Adrian -red, Wladimir - purple, Samantha - orange. 2. In other words: index 0 from the array of people assign (connect) to the index 0 from the colors array, index 1 from the people array assign(connect) with index 1 from the colors array, index 2 from the people array assign(connect) to index 2 from the colors array; and so on
Example code: http://plnkr.co/edit/TPZjXkkSRrIc5ApzP07F?p=preview
index.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="vm.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: {{vm.multipleDemo}}</p>
<hr>
</body>
</html>
**demo.js**
'use strict';
var app = angular.module('demo', ['ngSanitize', 'ui.select']);
app.controller('DemoCtrl', function($scope, $http, $timeout) {
$scope.vm = {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' }
];
$scope.colors = [
'pink', 'yellow', 'red', 'purple', 'orange', 'green'
];
$scope.OnClickSelect=function(item)
{
$scope.vm.multipleDemo.push(item.name);
}
$scope.OnRemoveSelect = function(item) {
window.console.log($scope.people);
var index = $scope.people.indexOf(item.name);
$scope.people.splice(index, 1);
}
$scope.clearTag = function() {
/*for(var i =0; i < $scope.vm.multipleDemo.length; i++) {
$scope.vm.multipleDemo.splice($scope.vm.multipleDemo[i]);
$scope.people.push($scope.vm.multipleDemo[i]);
}*/
var i =0;
while(i < $scope.vm.multipleDemo.length) {
i++;
$scope.vm.multipleDemo.splice($scope.vm.multipleDemo[i]);
$scope.people.push($scope.vm.multipleDemo[i]);
}
}
});```