AngularJS : Transclude a single input element into a directive template without using a container -
i create wrapper input field input-help tool tip inside of it.
i using angular 1.0.7, if it's significant.
i'm using transclude: true, along isolate scope in order allow errors @ several different fields simultaneously, , still maintain ng-model reference external $scope.
the problem:
when use directive on input element, input element doesn't clone('transclude') directive template.
as result of getting empty div element @ dom, ng-transclude attribute.
plunk: http://plnkr.co/edit/vfb9ih6x2nbmwhaes3qh?p=preview
code:
<input data-my-validate-input data-value-required="true" type="password" class="loginitem" placeholder="password" name="password" data-ng-model="formdata.password" data-display-name="password">
however when wrap input element in span or div, child input element transcended fine, don't reference correct external ng-model(ctrl) @ directive.
<span data-my-validate-input data-value-required="true" data-display-name="password"> <input type="password" class="loginitem" placeholder="password" name="password" data-ng-model="formdata.password" > </span>
full code(the logic inside link function not relevant problem, preferred post full code)
directive('myvalidateinput', function() { return { require: 'ngmodel', restrict: 'a', transclude: true, scope: { displayname: '@', valuerequired: '@', maxlength: '@', minlength: '@', minletters: '@', minnumbers: '@' }, template: '<div class="validationcontainer">\ <div ng-transclude></div>\ <div class="input-help">\ <h4>{{fielderrordisplay}}</h4>\ <ul>\ <li data-ng-repeat="rule in requirementspec" ng-class="rule.class">\ {{rule.msg}}\ </li>\ </ul>\ </div>\ </div>', replace: true, link: function(scope, elm, attrs, ctrl) { var validator = function(viewvalue){ if(scope.valuerequired && viewvalue.length == 0 && (!scope.maxlength && !scope.minlength && !scope.minletters && !scope.minnumbers)){ scope.valid = false; scope.fielderrordisplay = scope.fieldname + ' required'; } else{ scope.fielderrordisplay = scope.fieldname + ' must meet following requirements: '; scope.requirementspec = []; if(scope.minlength){ var itemvalidity = viewvalue.length >= scope.minlength; scope.valid = !itemvalidity ? false : scope.valid; var item = { 'msg' : 'must @ least ' + scope.minlength + ' characters long', 'class' : itemvalidity ? 'valid' : undefined }; scope.requirementspec[namestr].push(item); } else if(scope.valuerequired){ var itemvalidity = viewvalue && viewvalue.length >= 1; scope.valid = !itemvalidity ? false : scope.valid; var item = { 'msg' : 'this field must filled', 'class' : itemvalidity ? 'valid' : undefined }; scope.requirementspec[namestr].push(item); } if(scope.maxlength){ var itemvalidity = viewvalue.length <= scope.maxlength; scope.valid = !itemvalidity ? false : scope.valid; var item = { 'msg' : 'must ' + scope.maxlength + ' characters long @ ', 'class' : itemvalidity ? 'valid' : undefined }; scope.requirementspec[namestr].push(item); } if(scope.minletters){ var itemvalidity = (viewvalue && /[a-z]/.test(viewvalue)); scope.valid = !itemvalidity ? false : scope.valid; var item = { 'msg' : 'must contain @ least ' + scope.minletters + ' letters', 'class' : itemvalidity ? 'valid' : undefined }; scope.requirementspec[namestr].push(item); } if(attrs.minnumbers){ var itemvalidity = (viewvalue && /\d/.test(viewvalue)); scope.valid = !itemvalidity ? false : scope.valid; var item = { 'msg' : 'must contain @ least' + attrs.minnumbers + ' numbers', 'class' : itemvalidity ? 'valid' : undefined }; scope.requirementspec[namestr].push(item); } } if(scope.valid) { ctrl.$setvalidity(namestr, true); return viewvalue; } else { ctrl.$setvalidity(namestr, false); return undefined; } } scope.requirementspec = {}; ctrl.$parsers.unshift(function(viewvalue) { return validator(viewvalue); }); ctrl.$formatters.unshift(function(viewvalue) { // var before = scope.$eval(attrs.validatebefore); if(viewvalue && viewvalue != "" && viewvalue.length > 0) return validator(viewvalue); }); }); } });
the solution: $transclude takes compiled content of element, not element self.
obviously lack understanding of significant detail in implementation.
Comments
Post a Comment