my-label-input directive
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 24 Jan 2013 00:05:55 +0000 (01:05 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 24 Jan 2013 00:20:54 +0000 (01:20 +0100)
app/js/directives.js
test/unit/directivesSpec.js

index cac0c3c..88d5653 100644 (file)
@@ -25,6 +25,30 @@ angular.module('myApp.directives', []).
                        if ( attrs.bsTypeahead )  html += ' bs-typeahead="'+attrs.bsTypeahead+'"';
                        if ( attrs.dataItems )  html += ' data-items="'+attrs.dataItems+'"'; // for typeahead
                        html += '/></div>';
+                       element.replaceWith(html);
+               }
+    };
+  }).
+
+  directive('myLabelInput', function() {
+    return {
+               restrict: 'C',
+               compile: function(element, attrs) {
+                       var my_class = attrs['class'] || '';
+                       my_class = my_class.replace(/ *my-input */,'');
+                       var my_type  = attrs['type']  || 'text';
+                       var m = attrs.ngModel;
+                       var html =
+'  <div class="control-group">'+
+'    <label class="control-label" for="'+m+'">'+attrs.label+'</label>'+
+'    <div class="controls">'+
+'      <input type="'+my_type+'" id="'+m+'" ng-model="'+m+'" placeholder="'+attrs.placeholder+'"'
+                       ;
+                       if ( attrs.ngRequired ) html += ' ng-required="' + attrs.ngRequired + '"';
+                       html += '>'+
+'    </div>'+
+'  </div>';
+
                        element.replaceWith(html);
                }
     };
index f2ccb87..134ff8f 100644 (file)
@@ -28,4 +28,12 @@ describe('directives', function() {
     });
   });
 
+  describe('my-label-input', function() {
+    it('div+label+input', function() {
+      inject(function($compile, $rootScope) {
+        var element = $compile('<div><input class="my-label-input" ng-model="m" placeholder="p" label="label"></input></div>')($rootScope);
+        expect(element.html()).toMatch(/div.*label/i);
+      });
+    });
+  });
 });