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

fix(ngInput): When 'name' attribute is a property name, '$name' should be evaluated to the property value #4086

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,12 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
this.$dirty = false;
this.$valid = true;
this.$invalid = false;
this.$name = $attr.name;
try {
var attrName = $scope.$eval($attr.name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be done through $attrs.$observe and it needs to update the forms each time attribute changes.

this.$name = isUndefined(attrName) ? $attr.name : attrName;
} catch (e) {
this.$name = $attr.name;
}

var ngModelGet = $parse($attr.ngModel),
ngModelSet = ngModelGet.assign;
Expand Down
13 changes: 11 additions & 2 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ describe('NgModelController', function() {
});


it('should fail on non-assignable model binding', inject(function($controller) {
it('should fail on non-assignable model binding', inject(function($controller, $rootScope) {
var exception;

try {
$controller(NgModelController, {
$scope: null,
$scope: $rootScope,
$element: jqLite('<input ng-model="1+2">'),
$attrs: {
ngModel: '1+2'
Expand Down Expand Up @@ -62,6 +62,15 @@ describe('NgModelController', function() {
expect(ctrl.$name).toBe('testAlias');
});

it('should use the property value when name is a property name', inject(function($controller, $rootScope) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test case which would allow changing the name at runtime and showing that the old field is removed in the form and added under new name.

var scope = $rootScope.$new();
scope.testAlias = 'myAlias';
var controller = $controller(NgModelController, {
$scope: scope, $element: element.find('input'), $attrs: {name: 'testAlias', ngModel: 'value'}
});
expect(controller.$name).toBe(scope.testAlias);
}));


describe('setValidity', function() {

Expand Down