diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index fa953419c02c..9f7f7626aede 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -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); + this.$name = isUndefined(attrName) ? $attr.name : attrName; + } catch (e) { + this.$name = $attr.name; + } var ngModelGet = $parse($attr.ngModel), ngModelSet = ngModelGet.assign; diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index facc2b806bc9..09b52df1053f 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -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(''), $attrs: { ngModel: '1+2' @@ -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) { + 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() {