1
1
angular . module ( 'field-directive' , [ '1820EN_10_Code/04_field_directive/template/text.html' , '1820EN_10_Code/04_field_directive/template/number.html' ] )
2
2
3
- . directive ( 'field' , function ( $compile , $http , $templateCache ) {
3
+ . directive ( 'field' , function ( $compile , $http , $templateCache , $interpolate ) {
4
4
5
5
return {
6
6
restrict :'E' ,
@@ -14,7 +14,7 @@ angular.module('field-directive', ['1820EN_10_Code/04_field_directive/template/t
14
14
// (we need to replace dots with something to work with browsers and also form scope)
15
15
modelId = attrs . ngModel . replace ( '.' , '_' ) . toLowerCase ( ) ;
16
16
17
- // Load up the relevant template
17
+ // Load up the template for this kind of field
18
18
getFieldElement = $http . get ( '1820EN_10_Code/04_field_directive/template/' + attrs . type + '.html' , { cache :$templateCache } ) . then ( function ( response ) {
19
19
var newElement = angular . element ( response . data ) ;
20
20
var inputElement = newElement . find ( 'input' ) || newElement . find ( 'textarea' ) || newElement . find ( 'select' ) ;
@@ -40,34 +40,33 @@ angular.module('field-directive', ['1820EN_10_Code/04_field_directive/template/t
40
40
inputElement . attr ( 'id' , childScope . id ) ;
41
41
newElement . find ( 'label' ) . attr ( 'for' , childScope . id ) ;
42
42
43
- childScope . $messages = { } ;
43
+ childScope . $validationMessages = { } ;
44
44
var originalElement = transclude ( scope ) ;
45
45
angular . forEach ( originalElement . find ( 'validator' ) , function ( validatorElement ) {
46
46
validatorElement = angular . element ( validatorElement ) ;
47
- childScope . $messages [ validatorElement . attr ( 'key' ) ] = validatorElement . text ( ) ;
48
- var validationAttributes = scope . $eval ( validatorElement . attr ( 'attributes' ) ) ;
49
- angular . forEach ( validationAttributes , function ( key , value ) {
50
- inputElement . attr ( key , value ) ;
47
+
48
+ // We need to watch the message incase it has interpolated values that need processing
49
+ scope . $watch ( $interpolate ( validatorElement . text ( ) ) , function ( message ) {
50
+ childScope . $validationMessages [ validatorElement . attr ( ' key' ) ] = message ;
51
51
} ) ;
52
- } ) ;
53
52
54
- childScope . $watch ( '$field.$error' , function ( errors ) {
55
- childScope . $messages = [ ] ;
56
- angular . forEach ( errors , function ( key , value ) {
57
- if ( value ) {
58
- childScope . $messages . push ( childScope . $messages [ key ] ) ;
59
- }
53
+ // Extract the options and bind them to the input element
54
+ var validationAttributes = scope . $eval ( validatorElement . attr ( 'options' ) ) ;
55
+ angular . forEach ( validationAttributes , function ( value , key ) {
56
+ inputElement . attr ( key , value ) ;
60
57
} ) ;
61
- } , true ) ;
58
+ } ) ;
62
59
63
- // We must compile here otherwise the new input won't pick up the FormController
60
+ // We must compile in the postLink function rather than the compile function
61
+ // otherwise the new input won't pick up the FormController
64
62
$compile ( newElement ) ( childScope , function ( clone ) {
65
63
// We can only add the new element after the directive element because
66
64
// transclusion caused the directive element to be converted to a template.
67
65
// Comments are ignored by ng-repeat, otherwise this would not work
68
66
element . after ( clone ) ;
69
67
} ) ;
70
68
69
+ // Only after the new element has been compiled will we have access to the $field
71
70
if ( formController ) {
72
71
childScope . $form = formController ;
73
72
childScope . $field = formController [ childScope . name ] ;
0 commit comments