-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
Interpolates the form and form control attribute name, so that dynamic form controls (such as those rendered in an ngRepeat) will always have their expected interpolated name. The control will be present in its parent form controller with the interpolated property name, and this name can change when the interpolated value changes. Closes #4791 Closes #1404
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
*/ | ||
var nullFormCtrl = { | ||
$addControl: noop, | ||
$$renameControl: nullFormRenameControl, | ||
$removeControl: noop, | ||
$setValidity: noop, | ||
$$setPending: noop, | ||
|
@@ -14,6 +15,10 @@ var nullFormCtrl = { | |
}, | ||
SUBMITTED_CLASS = 'ng-submitted'; | ||
|
||
function nullFormRenameControl(control, name) { | ||
control.$name = name; | ||
} | ||
|
||
/** | ||
* @ngdoc type | ||
* @name form.FormController | ||
|
@@ -51,17 +56,18 @@ SUBMITTED_CLASS = 'ng-submitted'; | |
* | ||
*/ | ||
//asks for $scope to fool the BC controller module | ||
FormController.$inject = ['$element', '$attrs', '$scope', '$animate']; | ||
function FormController(element, attrs, $scope, $animate) { | ||
FormController.$inject = ['$element', '$attrs', '$scope', '$animate', '$interpolate']; | ||
function FormController(element, attrs, $scope, $animate, $interpolate) { | ||
var form = this, | ||
parentForm = element.parent().controller('form') || nullFormCtrl, | ||
controls = []; | ||
|
||
var parentForm = form.$$parentForm = element.parent().controller('form') || nullFormCtrl; | ||
|
||
// init state | ||
form.$error = {}; | ||
form.$$success = {}; | ||
form.$pending = undefined; | ||
form.$name = attrs.name || attrs.ngForm; | ||
form.$name = $interpolate(attrs.name || attrs.ngForm || '')($scope); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
caitp
Author
Contributor
|
||
form.$dirty = false; | ||
form.$pristine = true; | ||
form.$valid = true; | ||
|
@@ -127,6 +133,17 @@ function FormController(element, attrs, $scope, $animate) { | |
} | ||
}; | ||
|
||
// Private API: rename a form control | ||
form.$$renameControl = function(control, newName) { | ||
var oldName = control.$name; | ||
|
||
if (form[oldName] === control) { | ||
delete form[oldName]; | ||
} | ||
form[newName] = control; | ||
control.$name = newName; | ||
}; | ||
|
||
/** | ||
* @ngdoc method | ||
* @name form.FormController#$removeControl | ||
|
@@ -466,13 +483,20 @@ var formDirectiveFactory = function(isNgForm) { | |
}); | ||
} | ||
|
||
var parentFormCtrl = formElement.parent().controller('form'), | ||
alias = attr.name || attr.ngForm; | ||
var parentFormCtrl = controller.$$parentForm, | ||
alias = controller.$name; | ||
|
||
if (alias) { | ||
setter(scope, alias, controller, alias); | ||
attr.$observe(attr.name ? 'name' : 'ngForm', function(newValue) { | ||
if (alias === newValue) return; | ||
setter(scope, alias, undefined, alias); | ||
alias = newValue; | ||
setter(scope, alias, controller, alias); | ||
parentFormCtrl.$$renameControl(controller, alias); | ||
}); | ||
This comment has been minimized.
Sorry, something went wrong.
dragosrususv
|
||
} | ||
if (parentFormCtrl) { | ||
if (parentFormCtrl !== nullFormCtrl) { | ||
formElement.on('$destroy', function() { | ||
parentFormCtrl.$removeControl(controller); | ||
if (alias) { | ||
|
Aren't the values in the attributes object already interpolated? Why call $interpolate here?Oh, now I see that during controller initialization they're not interpolated yet. Is it documented?