6 angular.module('myApp.directives', []).
7 directive('appVersion', ['version', function(version) {
8 return function(scope, elm, attrs) {
12 directive('myInput', function() {
15 compile: function(element, attrs) {
16 var my_class = attrs['class'] || '';
17 my_class = my_class.replace(/ *my-input */,'');
18 var my_type = attrs['type'] || 'text';
19 var html = '<div class="'+my_class+'">'
20 +'<label for="'+attrs.ngModel+'">' + attrs.placeholder + '</label>'
21 +'<input type="'+my_type+'" name="'+attrs.ngModel+'" ng-model="' + attrs.ngModel + '"'
22 +' placeholder="'+attrs.placeholder+'" class="'+my_class+'"'
24 if ( attrs.ngRequired ) html += ' ng-required="' + attrs.ngRequired + '"';
25 if ( attrs.bsTypeahead ) html += ' bs-typeahead="'+attrs.bsTypeahead+'"';
26 if ( attrs.dataItems ) html += ' data-items="'+attrs.dataItems+'"'; // for typeahead
28 element.replaceWith(html);
32 .directive('bsTypeahead', ['$parse', function($parse) {
38 link: function postLink(scope, element, attr, controller) {
40 var getter = $parse(attr.bsTypeahead),
41 setter = getter.assign,
42 value = getter(scope);
44 // Watch bsTypeahead for changes
45 scope.$watch(attr.bsTypeahead, function(newValue, oldValue) {
46 if(newValue !== oldValue) {
51 element.attr('data-provide', 'typeahead');
53 source: function(query) { return value; },
55 updater: function(value) {
56 // If we have a controller (i.e. ngModelController) then wire it up
58 scope.$apply(function () {
59 controller.$setViewValue(value);
66 // add entered element into typeahead array for other fields
67 element.bind('blur', function() {
68 var new_value = element.val();
69 if ( new_value.length > 1 && $.inArray( new_value, value ) === -1 ) { // IE doesn't have .indexOf
70 scope.$apply( function() {
71 value.unshift( element.val() );