From 13ee11a887c5ab36e9860b7707261c3784c0fe43 Mon Sep 17 00:00:00 2001 From: Pasvaz Date: Mon, 17 Jun 2013 16:42:47 +0200 Subject: [PATCH] feat(ngName): allow interpolated name for form controls ngName allows forms' fields to set their name dynamically, especially useful with ngRepeat. Right now is not possible to validate form controls like this: The current work-around is to use one nested form for each field but indeed it's an hack rather than a solution. This implementation could be moved inside the directive 'ngModel' or within its own directive 'ngName', I left intentionally it in the NgModelController because it's slightly faster, however I can refactor it as you prefer. this fixes also the issues #1404 and #2426 --- src/ng/directive/input.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 591da99e3b8d..4b3f9583f5b0 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -924,6 +924,23 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ this.$invalid = false; this.$name = $attr.name; + // model -> value + var ctrl = this; + + if (isDefined($attr.ngName)) { + ctrl.$name = $scope.$eval($attr.ngName); + $attr.$set('name', ctrl.$name); + $attr.$observe('ngName', function() { + var newName = $scope.$eval($attr.ngName); + if (newName != ctrl.$name) { + parentForm.$removeControl(ctrl); + ctrl.$name = newName; + $attr.$set('name', ctrl.$name); + parentForm.$addControl(ctrl); + } + }); + } + var ngModelGet = $parse($attr.ngModel), ngModelSet = ngModelGet.assign; @@ -1063,9 +1080,6 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ } }; - // model -> value - var ctrl = this; - $scope.$watch(function ngModelWatch() { var value = ngModelGet($scope);