1
- angular . module ( 'field-directive' , [
2
- '1820EN_10_Code/04_field_directive/template/input.html' ,
3
- '1820EN_10_Code/04_field_directive/template/textarea.html' ,
4
- '1820EN_10_Code/04_field_directive/template/select.html'
5
- ] )
1
+ angular . module ( 'field-directive' , [ 'input.html' , 'textarea.html' , 'select.html' ] )
6
2
7
- . directive ( 'field' , function ( $compile , $http , $templateCache , $interpolate ) {
3
+ . factory ( 'loadTemplate' , function ( $http , $templateCache ) {
4
+ return function ( template ) {
5
+ return $http . get ( template , { cache :$templateCache } ) . then ( function ( response ) {
6
+ return angular . element ( response . data ) ;
7
+ } , function ( response ) {
8
+ throw new Error ( 'Template not found: ' + template ) ;
9
+ } ) ;
10
+ } ;
11
+ } )
12
+
13
+ . directive ( 'field' , function ( $compile , $http , $interpolate , loadTemplate ) {
8
14
9
15
var findInputElement = function ( element ) {
10
16
return angular . element ( element . find ( 'input' ) [ 0 ] || element . find ( 'textarea' ) [ 0 ] || element . find ( 'select' ) [ 0 ] ) ;
@@ -28,26 +34,29 @@ angular.module('field-directive', [
28
34
} ) ;
29
35
30
36
// Find the content that will go into the new label
37
+ // (attribute trumps child element)
31
38
var labelContent = '' ;
32
39
if ( element . attr ( 'label' ) ) {
40
+ // Label is provided as an attribute on the <field> element
33
41
labelContent = element . attr ( 'label' ) ;
34
42
element [ 0 ] . removeAttribute ( 'label' ) ;
35
- }
36
- if ( element . find ( ' label' ) [ 0 ] ) {
43
+ } else if ( element . find ( 'label' ) [ 0 ] ) {
44
+ // Label is provided as a < label> child element of the <field> element
37
45
labelContent = element . find ( 'label' ) . html ( ) ;
38
46
}
39
47
if ( ! labelContent ) {
40
48
throw new Error ( 'No label provided' ) ;
41
49
}
42
50
51
+ element . html ( '' ) ;
52
+
43
53
// Load up the template for this kind of field
44
- var template = attrs . template || 'input' ; // Default to the simple input if none given
45
- var getFieldElement = $http . get ( '1820EN_10_Code/04_field_directive/template/' + template + '.html' , { cache :$templateCache } ) . then ( function ( response ) {
46
- var newElement = angular . element ( response . data ) ;
47
- var inputElement = findInputElement ( newElement ) ;
54
+ var template = attrs . template || 'input.html' ; // Default to the simple input if none given
55
+ var getFieldElement = loadTemplate ( template ) . then ( function ( newElement ) {
48
56
49
57
// Copy over the attributes to the input element
50
- // At least the ng-model attribute must be copied because we can't use interpolation in the template
58
+ // We can't use interpolation in the template for directives such as ng-model
59
+ var inputElement = findInputElement ( newElement ) ;
51
60
angular . forEach ( element [ 0 ] . attributes , function ( attribute ) {
52
61
var value = attribute . value ;
53
62
var key = attribute . name ;
@@ -95,10 +104,8 @@ angular.module('field-directive', [
95
104
// (i.e. after any parent form element has been linked)
96
105
// otherwise the new input won't pick up the FormController
97
106
$compile ( newElement ) ( childScope , function ( clone ) {
98
- // Place our new element after the original element
99
- element . after ( clone ) ;
100
- // Remove our original element
101
- element . remove ( ) ;
107
+ // Place our new element as a child of the original element
108
+ element . append ( clone ) ;
102
109
} ) ;
103
110
104
111
// Only after the new element has been compiled do we have access to the ngModelController
0 commit comments