Skip to content

Instantly share code, notes, and snippets.

@wejendorp
Created January 2, 2014 10:35
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 wejendorp/8217453 to your computer and use it in GitHub Desktop.
Save wejendorp/8217453 to your computer and use it in GitHub Desktop.
Angular bootstrap tags via modelController
angular.module('bootstrap-tagsinput', [])
.directive('tagsinput', [function() {
function getItemProperty(scope, property) {
if (!property)
return undefined;
if (angular.isFunction(scope.$parent[property]))
return scope.$parent[property];
return function(item) {
return item[property];
};
}
return {
require: '^ngModel',
restrict: 'A',
link: function(scope, element, attrs, ctrl) {
// view -> model
ctrl.$parsers.unshift(function () {
return $(element).tagsinput('items');
});
$(function() {
var select = $(element);
select.tagsinput({
typeahead : {
source : angular.isFunction(scope.$parent[attrs.typeaheadSource]) ? scope.$parent[attrs.typeaheadSource] : null
},
itemValue: getItemProperty(scope, attrs.itemvalue),
itemText : getItemProperty(scope, attrs.itemtext),
tagClass : angular.isFunction(scope.$parent[attrs.tagclass]) ? scope.$parent[attrs.tagclass] : function(item) { return attrs.tagclass; }
});
select.on('itemAdded', function(event) {
scope.$apply();
});
select.on('itemRemoved', function(event) {
scope.$apply();
});
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment