File tree 2 files changed +31
-3
lines changed
2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -1110,7 +1110,21 @@ var ngModelDirective = function() {
1110
1110
var modelCtrl = ctrls [ 0 ] ,
1111
1111
formCtrl = ctrls [ 1 ] || nullFormCtrl ;
1112
1112
1113
- formCtrl . $addControl ( modelCtrl ) ;
1113
+ // Support of the interpolation in the name attribute,
1114
+ // when the name changes, the control is republished in the form.
1115
+ // We rely on the behaviour of the attribute interpolation directive,
1116
+ // which sets the value to undefined during compilation.
1117
+ if ( attr . name ) {
1118
+ formCtrl . $addControl ( modelCtrl ) ;
1119
+ } else {
1120
+ attr . $observe ( 'name' , function ( name ) {
1121
+ if ( modelCtrl . $name ) {
1122
+ formCtrl . $removeControl ( modelCtrl ) ;
1123
+ }
1124
+ modelCtrl . $name = name ;
1125
+ formCtrl . $addControl ( modelCtrl ) ;
1126
+ } ) ;
1127
+ }
1114
1128
1115
1129
element . bind ( '$destroy' , function ( ) {
1116
1130
formCtrl . $removeControl ( modelCtrl ) ;
Original file line number Diff line number Diff line change @@ -111,8 +111,7 @@ describe('form', function() {
111
111
expect ( scope . formB . $error . required ) . toBe ( false ) ;
112
112
} ) ;
113
113
114
-
115
- it ( 'should publish widgets' , function ( ) {
114
+ it ( 'should publish widgets with static names' , function ( ) {
116
115
doc = jqLite ( '<form name="form"><input type="text" name="w1" ng-model="some" /></form>' ) ;
117
116
$compile ( doc ) ( scope ) ;
118
117
@@ -124,6 +123,21 @@ describe('form', function() {
124
123
expect ( widget . $invalid ) . toBe ( false ) ;
125
124
} ) ;
126
125
126
+ it ( 'should publish widgets with dynamic names' , function ( ) {
127
+ doc = jqLite ( '<form name="form"><input type="text" name="{{name}}" ng-model="some" /></form>' ) ;
128
+ $compile ( doc ) ( scope ) ;
129
+
130
+ scope . name = 'w1' ;
131
+ scope . $digest ( ) ;
132
+ var widget = scope . form . w1 ;
133
+ expect ( widget ) . toBeDefined ( ) ;
134
+
135
+ scope . name = 'w2' ;
136
+ scope . $digest ( ) ;
137
+ expect ( scope . form . w1 ) . toBeUndefined ( ) ;
138
+ expect ( scope . form . w2 ) . toBeDefined ( ) ;
139
+ expect ( scope . form . w2 ) . toBe ( widget ) ;
140
+ } ) ;
127
141
128
142
describe ( 'preventing default submission' , function ( ) {
129
143
You can’t perform that action at this time.
0 commit comments