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,17 @@ 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
+
130
146
/**
131
147
* @ngdoc method
132
148
* @name form.FormController#$removeControl
@@ -466,13 +482,20 @@ var formDirectiveFactory = function(isNgForm) {
466
482
} ) ;
467
483
}
468
484
469
- var parentFormCtrl = formElement . parent ( ) . controller ( 'form' ) ,
470
- alias = attr . name || attr . ngForm ;
485
+ var parentFormCtrl = formElement . parent ( ) . controller ( 'form' ) || nullFormCtrl ,
486
+ alias = controller . $ name;
471
487
472
488
if ( alias ) {
473
489
setter ( scope , alias , controller , alias ) ;
490
+ attr . $observe ( attr . name ? 'name' : 'ngForm' , function ( newValue ) {
491
+ if ( alias === newValue ) return ;
492
+ setter ( scope , alias , undefined , alias ) ;
493
+ alias = newValue ;
494
+ setter ( scope , alias , controller , alias ) ;
495
+ parentFormCtrl . $$renameControl ( controller , alias ) ;
496
+ } ) ;
474
497
}
475
- if ( parentFormCtrl ) {
498
+ if ( parentFormCtrl !== nullFormCtrl ) {
476
499
formElement . on ( '$destroy' , function ( ) {
477
500
parentFormCtrl . $removeControl ( controller ) ;
478
501
if ( alias ) {
0 commit comments