ngModel renames inputs with no "name" attribute to "undefined" #9707
Description
The following Plunker illustrates the issue (see the console output).
http://plnkr.co/edit/lZEgQv05Dm7YGelXZDhg?p=preview
When an input uses the ng-model directive, the following code watches for the "name" attribute to change.
src/ng/directive/input.js:2456
attr.$observe('name', function(newValue) {
if (modelCtrl.$name !== newValue) {
formCtrl.$$renameControl(modelCtrl, newValue);
}
});
introduced: 729c238
modified: b1ee538
The problem is that this observer fires with undefined
if the input doesn't have a "name" attribute, causing the control to be renamed to undefined
. If multiple inputs are missing the "name" attribute, they are all renamed to undefined
, and overwrite each other in the formCtrl
object.
I propose a fix:
attr.$observe('name', function(newValue) {
if (undefined !== newValue && modelCtrl.$name !== newValue) {
formCtrl.$$renameControl(modelCtrl, newValue);
}
});
And a test that fails before the fix and passes after the fix:
// test/ng/directive/inputSpec.js
it('should not rename form controls in form when name is undefined', function() {
compileInput('<input type="text" ng-model="name" />');
expect(scope.form['undefined']).toBeUndefined();
});
I'm happy to submit this as a PR, but I wanted to file an issue first to confirm that this is unexpected behavior, and that my fix looks OK.