angularjs - How to add a class css into directive? -
i have directive use change text of input uppercase
, works fine, want show text capitalize
when user type, note want keep text uppercase show user should capitalize. created css class don't know how use css directive.
how add css class directive ?
trying.
css
.textcapitalize { text-transform: capitalize; }
directive
var app = angular.module('starter'); app.directive('uiuppercase', function(){ return { require:'ngmodel', link: function(scope, element, attr, ctrl){ element.addclass('textcapitalize'); //show text capitalized var _textformat = function(input){ return input.length > 0 ? input.touppercase() : ""; } element.bind('keyup', function(){ ctrl.$setviewvalue(_textformat(ctrl.$viewvalue)); ctrl.$render(); }); } }; });
try this:
app.directive('uiuppercase', function() { return { restrict: 'a', require: 'ngmodel', link: function(scope, element, attr, ctrl) { function adduppercaseclass(ngmodelvalue) { if(!element.hasclass("textcapitalize")) element.addclass("textcapitalize"); } ctrl.$parsers.push(adduppercaseclass); } }; });
Comments
Post a Comment