angularjs - Make a directive with minimal length "required" -



angularjs - Make a directive with minimal length "required" -

i have directive adds % every input. want able create "required". if % exists invalid. additional requirement, input field can enabled/ disabled user (with ngdisabled). when disabled don't need create "required".

i have tried set ngminlength=1, doesnt show % want, when input empty, because invalid. how can it?

http://jsfiddle.net/jivangilad/uzvn3/

<input valid-num type="text" ng-model="deal.cupon"> var portaldirectives = angular.module("portaldirectives", []); portaldirectives.directive("validnum", function($filter) { homecoming { restrict: 'a', require: '?ngmodel', priority: 1, link: function(scope, element, attr, ngmodelctrl) { ngmodelctrl.$parsers.unshift(function(val) { if (val === undefined) val = ""; var formatedstr = val; if (val.indexof("%") == -1) formatedstr = val + "%"; if (formatedstr !== val) { ngmodelctrl.$setviewvalue(formatedstr); ngmodelctrl.$render(); } var num = formatedstr.tostring().replace(/\,/g, '').replace('%', ''); homecoming num; }); ngmodelctrl.$formatters.unshift(function(viewvalue) { homecoming viewvalue + "%"; }); } }; });

i'd suggest utilize design instead of logic problem. this:

other way not add together percent if viewvalue falsey, work

ngmodelctrl.$formatters.unshift(function(viewvalue) { homecoming viewvalue? viewvalue + "%" : ''; });

angularjs

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -