4
4
*/
5
5
var nullFormCtrl = {
6
6
$addControl : noop ,
7
+ $$renameControl : nullFormRenameControl ,
7
8
$removeControl : noop ,
8
9
$setValidity : noop ,
9
10
$$setPending : noop ,
@@ -14,6 +15,10 @@ var nullFormCtrl = {
14
15
} ,
15
16
SUBMITTED_CLASS = 'ng-submitted' ;
16
17
18
+ function nullFormRenameControl ( control , name ) {
19
+ control . $name = name ;
20
+ }
21
+
17
22
/**
18
23
* @ngdoc type
19
24
* @name form.FormController
@@ -51,8 +56,8 @@ SUBMITTED_CLASS = 'ng-submitted';
51
56
*
52
57
*/
53
58
//asks for $scope to fool the BC controller module
54
- FormController . $inject = [ '$element' , '$attrs' , '$scope' , '$animate' ] ;
55
- function FormController ( element , attrs , $scope , $animate ) {
59
+ FormController . $inject = [ '$element' , '$attrs' , '$scope' , '$animate' , '$interpolate' ] ;
60
+ function FormController ( element , attrs , $scope , $animate , $interpolate ) {
56
61
var form = this ,
57
62
parentForm = element . parent ( ) . controller ( 'form' ) || nullFormCtrl ,
58
63
controls = [ ] ;
@@ -61,7 +66,7 @@ function FormController(element, attrs, $scope, $animate) {
61
66
form . $error = { } ;
62
67
form . $$success = { } ;
63
68
form . $pending = undefined ;
64
- form . $name = attrs . name || attrs . ngForm ;
69
+ form . $name = $interpolate ( attrs . name || attrs . ngForm || '' ) ( $scope ) ;
65
70
form . $dirty = false ;
66
71
form . $pristine = true ;
67
72
form . $valid = true ;
@@ -127,6 +132,19 @@ function FormController(element, attrs, $scope, $animate) {
127
132
}
128
133
} ;
129
134
135
+ // Private API: rename a form control
136
+ form . $$renameControl = function ( control , newName ) {
137
+ var oldName = control . $name ;
138
+
139
+ if ( form [ oldName ] === control ) {
140
+ delete form [ oldName ] ;
141
+ }
142
+ form [ newName ] = control ;
143
+ control . $name = newName ;
144
+ } ;
145
+
146
+ form . $$parentForm = parentForm ;
147
+
130
148
/**
131
149
* @ngdoc method
132
150
* @name form.FormController#$removeControl
@@ -466,13 +484,20 @@ var formDirectiveFactory = function(isNgForm) {
466
484
} ) ;
467
485
}
468
486
469
- var parentFormCtrl = formElement . parent ( ) . controller ( 'form' ) ,
470
- alias = attr . name || attr . ngForm ;
487
+ var parentFormCtrl = controller . $$parentForm ,
488
+ alias = controller . $ name;
471
489
472
490
if ( alias ) {
473
491
setter ( scope , alias , controller , alias ) ;
492
+ attr . $observe ( attr . name ? 'name' : 'ngForm' , function ( newValue ) {
493
+ if ( alias === newValue ) return ;
494
+ setter ( scope , alias , undefined , alias ) ;
495
+ alias = newValue ;
496
+ setter ( scope , alias , controller , alias ) ;
497
+ parentFormCtrl . $$renameControl ( controller , alias ) ;
498
+ } ) ;
474
499
}
475
- if ( parentFormCtrl ) {
500
+ if ( parentFormCtrl !== nullFormCtrl ) {
476
501
formElement . on ( '$destroy' , function ( ) {
477
502
parentFormCtrl . $removeControl ( controller ) ;
478
503
if ( alias ) {
0 commit comments