You need to add a new name to the drop-down list (using the ui-grid ). It is necessary that when you click on the addNewPerson button, the value by id is taken from the input field and “pushed” to the drop-down list if such a name does not already exist. If there is such a name, alert should be called.
Here is a piece of code responsible for creating a drop-down list in html :
<ui-select ng-model="person.selected" theme="select2" style="min-width:300px;"> <ui-select-match placeholder="Select a person in the list or search by name">{{$select.selected.name}} </ui-select-match> <ui-select-choices repeat="person in contacts | filter: {name: $select.search} track by $index"> <div ng-bind-html="person.name | highlight: $select.search"></div> </ui-select-choices> </ui-select> Button and input field:
<button type="button" id="addPerson" class="button" ng- click="addNewPerson()">Add New Person</button> <input id="name" class="form-control" placeholder="Enter your Name"> An array of contacts objects with a name field that is passed to the drop-down list:
$scope.contacts = [ {name: "Han Solo"}, {name: "ThetaSigma"}, {name: "Ollie Reeder"}, {name: "Amy McDonald"}, {name: "PJ Harvey"}, {name: "Sofie Marceau"}, {name: "Arthur Zimmermann"}, {name: "Michelle Dockery"}, {name: "Xavier Dolan"} ]; And finally, the function responsible for adding:
$scope.person = {}; $scope.addNewPerson = function () { var nameInput = document.getElementById("name"); for (var i=0; i <= $scope.contacts.length; i++) { if ($scope.contacts[i].name == nameInput.value.toLowerCase()) { alert("Error, the name entered already exists"); }else{ var obj1 = {name: nameInput.value}; $scope.contacts.push(obj1); } } }; While this function does not add anything and issues an alert 10 times, I do not understand why.