Last active
December 19, 2015 06:58
-
-
Save edinella/5914728 to your computer and use it in GitHub Desktop.
Diretiva para adicionar campos com name dinâmico ao form, para validação
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* https://groups.google.com/forum/?fromgroups=#!topic/angular/suCtNH10M2U | |
* | |
* <p ng-repeat="campo in campos"> | |
* <label>{{campo.label}}</label> | |
* <input ng-name="campo.name" /> | |
* </p> | |
* | |
* formName[fieldName].$error.required | |
*/ | |
.directive('ngName', ['$interpolate', function ($interpolate) { | |
return { | |
restrict: 'A', | |
require: ['ngModel', '^?form'], | |
link: function (scope, element, attrs, ctrl) { | |
var modelCtrl = ctrl[0]; | |
var formCtrl = ctrl[1] || {}; | |
modelCtrl.$name = ($interpolate('{{'+element.attr(attrs.$attr.ngName)+'}}'))(scope); | |
return formCtrl.$addControl(modelCtrl); | |
} | |
}; | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment