@@ -5,41 +5,71 @@ angular
5
5
function MdAutocompleteItemScopeDirective ( $compile , $mdUtil ) {
6
6
return {
7
7
restrict : 'AE' ,
8
- link : postLink ,
9
- terminal : true
8
+ compile : compile ,
9
+ terminal : true ,
10
+ transclude : 'element'
10
11
} ;
11
12
12
- function postLink ( scope , element , attr ) {
13
- var ctrl = scope . $mdAutocompleteCtrl ;
14
- var newScope = ctrl . parent . $new ( ) ;
15
- var itemName = ctrl . itemName ;
16
-
17
- // Watch for changes to our scope's variables and copy them to the new scope
18
- watchVariable ( '$index' , '$index' ) ;
19
- watchVariable ( 'item' , itemName ) ;
20
-
21
- // Recompile the contents with the new/modified scope
22
- $compile ( element . contents ( ) ) ( newScope ) ;
23
-
24
- // Replace it if required
25
- if ( attr . hasOwnProperty ( 'mdAutocompleteReplace' ) ) {
26
- element . after ( element . contents ( ) ) ;
27
- element . remove ( ) ;
28
- }
29
-
30
- /**
31
- * Creates a watcher for variables that are copied from the parent scope
32
- * @param variable
33
- * @param alias
34
- */
35
- function watchVariable ( variable , alias ) {
36
- newScope [ alias ] = scope [ variable ] ;
37
-
38
- scope . $watch ( variable , function ( value ) {
39
- $mdUtil . nextTick ( function ( ) {
40
- newScope [ alias ] = value ;
41
- } ) ;
13
+ function compile ( tElement , tAttr , transclude ) {
14
+ return function postLink ( scope , element , attr ) {
15
+ var ctrl = scope . $mdAutocompleteCtrl ;
16
+ var newScope = ctrl . parent . $new ( ) ;
17
+ var itemName = ctrl . itemName ;
18
+
19
+ // Watch for changes to our scope's variables and copy them to the new scope
20
+ watchVariable ( '$index' , '$index' ) ;
21
+ watchVariable ( 'item' , itemName ) ;
22
+
23
+ // Ensure that $digest calls on our scope trigger $digest on newScope.
24
+ connectScopes ( ) ;
25
+
26
+ // Link the element against newScope.
27
+ transclude ( newScope , function ( clone ) {
28
+ element . after ( clone ) ;
42
29
} ) ;
43
- }
30
+
31
+ /**
32
+ * Creates a watcher for variables that are copied from the parent scope
33
+ * @param variable
34
+ * @param alias
35
+ */
36
+ function watchVariable ( variable , alias ) {
37
+ newScope [ alias ] = scope [ variable ] ;
38
+
39
+ scope . $watch ( variable , function ( value ) {
40
+ $mdUtil . nextTick ( function ( ) {
41
+ newScope [ alias ] = value ;
42
+ } ) ;
43
+ } ) ;
44
+ }
45
+
46
+ /**
47
+ * Creates watchers on scope and newScope that ensure that for any
48
+ * $digest of scope, newScope is also $digested.
49
+ */
50
+ function connectScopes ( ) {
51
+ var scopeDigesting = false ;
52
+ var newScopeDigesting = false ;
53
+
54
+ scope . $watch ( function ( ) {
55
+ if ( newScopeDigesting || scopeDigesting ) {
56
+ return ;
57
+ }
58
+
59
+ scopeDigesting = true ;
60
+ scope . $$postDigest ( function ( ) {
61
+ if ( ! newScopeDigesting ) {
62
+ newScope . $digest ( ) ;
63
+ }
64
+
65
+ scopeDigesting = newScopeDigesting = false ;
66
+ } ) ;
67
+ } ) ;
68
+
69
+ newScope . $watch ( function ( ) {
70
+ newScopeDigesting = true ;
71
+ } ) ;
72
+ }
73
+ } ;
44
74
}
45
75
}
0 commit comments