Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ngModel renames inputs with no "name" attribute to "undefined" #9707

Closed
@itsananderson

Description

@itsananderson

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions