Last active
December 22, 2015 20:09
-
-
Save hitenpratap/8b1c34d0db5b2f4c41cd to your computer and use it in GitHub Desktop.
Tag input with autocomplete in AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tagApp = angular.module('tagApp',['ui.autocomplete']); | |
tagApp.controller('tagController',function($scope,EventService){ | |
$scope.tags = []; | |
$scope.addTag = function () { | |
if ($scope.modelObj != "" && $scope.tags.indexOf($scope.modelObj) == -1) { | |
$scope.tags.push($scope.modelObj); | |
} | |
$scope.modelObj = ""; | |
}; | |
$scope.removeTag = function (index) { | |
$scope.tags.splice(index, 1); | |
}; | |
//for more detail visit: https://github.com/zensh/ui-autocomplete | |
$scope.myOption = { | |
options: { | |
html: true, | |
focusOpen: false, | |
onlySelectValid: false, | |
//I'm getting data from remote data source here, you can use local data source instead as well | |
source: function (request, response) { | |
var data = []; | |
EventService.getTagList(request.term, function (result) { | |
data = result; | |
if (!data.length) { | |
data.push({ | |
label: 'Seems not to have the user registered. This user will be notified by email about this event.', | |
value: '' | |
}); | |
} | |
response(data); | |
}); | |
} | |
}, | |
methods: {} | |
}; | |
}); | |
tagApp.service('EventService', function ($http){ | |
this.getTagList = function (term, callback) { | |
$http.get("URL OF SERVER", {params: {term: term}}).success(function (data) { | |
callback(data); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment