Skip to content

Instantly share code, notes, and snippets.

@hitenpratap
Last active December 22, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitenpratap/8b1c34d0db5b2f4c41cd to your computer and use it in GitHub Desktop.
Save hitenpratap/8b1c34d0db5b2f4c41cd to your computer and use it in GitHub Desktop.
Tag input with autocomplete in AngularJS
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