Skip to content

Instantly share code, notes, and snippets.

@edinella
Last active December 19, 2015 06:58
Show Gist options
  • Save edinella/5914728 to your computer and use it in GitHub Desktop.
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
/*
* 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