@@ -67,7 +67,7 @@ angular.module('field-directive', ['input.html', 'textarea.html', 'select.html']
67
67
68
68
return function postLink ( scope , element , attrs ) {
69
69
// Load up the template for this kind of field, default to the simple input if none given
70
- loadTemplate ( attrs . template || 'input.html' ) . then ( function ( newElement ) {
70
+ loadTemplate ( attrs . template || 'input.html' ) . then ( function ( templateElement ) {
71
71
// Set up the scope - the template will have its own scope, which is a child of the directive's scope
72
72
var childScope = scope . $new ( ) ;
73
73
// Attach a copy of the message map to the scope
@@ -81,22 +81,17 @@ angular.module('field-directive', ['input.html', 'textarea.html', 'select.html']
81
81
// Update the $fieldErrors array when the validity of the field changes
82
82
childScope . $watch ( '$field.$dirty && $field.$error' , function ( errorList ) {
83
83
childScope . $fieldErrors = [ ] ;
84
- if ( errorList ) {
85
- angular . forEach ( errorList , function ( invalid , key ) {
86
- if ( invalid ) {
87
- childScope . $fieldErrors . push ( key ) ;
88
- }
89
- } ) ;
90
- }
84
+ angular . forEach ( errorList , function ( invalid , key ) {
85
+ if ( invalid ) {
86
+ childScope . $fieldErrors . push ( key ) ;
87
+ }
88
+ } ) ;
91
89
} , true ) ;
92
90
93
- // Update the label's contents
94
- var labelElement = newElement . find ( 'label' ) ;
95
- labelElement . html ( labelContent ) ;
96
91
97
92
// Copy over all left over attributes to the input element
98
93
// We can't use interpolation in the template for directives such as ng-model
99
- var inputElement = findInputElement ( newElement ) ;
94
+ var inputElement = findInputElement ( templateElement ) ;
100
95
angular . forEach ( attrs . $attr , function ( original , normalized ) {
101
96
var value = element . attr ( original ) ;
102
97
inputElement . attr ( original , value ) ;
@@ -107,14 +102,18 @@ angular.module('field-directive', ['input.html', 'textarea.html', 'select.html']
107
102
// If we leave it to be interpolated at the next $digest the formController doesn't pick it up
108
103
inputElement . attr ( 'name' , childScope . $fieldId ) ;
109
104
inputElement . attr ( 'id' , childScope . $fieldId ) ;
110
- newElement . find ( 'label' ) . attr ( 'for' , childScope . $fieldId ) ;
105
+ var labelElement = templateElement . find ( 'label' ) ;
106
+ labelElement . attr ( 'for' , childScope . $fieldId ) ;
107
+ // Update the label's contents
108
+ labelElement . html ( labelContent ) ;
109
+
110
+ // Place our template as a child of the original element.
111
+ // This needs to be done before compilation to ensure that it picks up any containing form.
112
+ element . append ( templateElement ) ;
111
113
112
114
// We now compile and link our template here in the postLink function
113
115
// This allows the ng-model directive on our template's <input> element to access the ngFormController
114
- $compile ( newElement ) ( childScope ) ;
115
-
116
- // Place our template as a child of the original element
117
- element . append ( newElement ) ;
116
+ $compile ( templateElement ) ( childScope ) ;
118
117
119
118
// Now that our template has been compiled and linked we can access the <input> element's ngModelController
120
119
childScope . $field = inputElement . controller ( 'ngModel' ) ;
0 commit comments