@@ -10,17 +10,13 @@ function MdAutocompleteItemScopeDirective($compile, $mdUtil) {
10
10
} ;
11
11
12
12
function postLink ( scope , element , attr ) {
13
- var newScope = scope . $mdAutocompleteCtrl . parent . $new ( ) ;
14
- var relevantVariables = [ 'item' , '$index' ] ;
13
+ var ctrl = scope . $mdAutocompleteCtrl ;
14
+ var newScope = ctrl . parent . $new ( ) ;
15
+ var itemName = ctrl . itemName ;
15
16
16
17
// Watch for changes to our scope's variables and copy them to the new scope
17
- angular . forEach ( relevantVariables , function ( variable ) {
18
- scope . $watch ( variable , function ( value ) {
19
- $mdUtil . nextTick ( function ( ) {
20
- newScope [ variable ] = value ;
21
- } ) ;
22
- } ) ;
23
- } ) ;
18
+ watchVariable ( '$index' , '$index' ) ;
19
+ watchVariable ( 'item' , itemName ) ;
24
20
25
21
// Recompile the contents with the new/modified scope
26
22
$compile ( element . contents ( ) ) ( newScope ) ;
@@ -30,5 +26,17 @@ function MdAutocompleteItemScopeDirective($compile, $mdUtil) {
30
26
element . after ( element . contents ( ) ) ;
31
27
element . remove ( ) ;
32
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
+ $mdUtil . nextTick ( function ( ) {
37
+ newScope [ alias ] = scope [ variable ] ;
38
+ scope . $watch ( variable , function ( value ) { newScope [ alias ] = value ; } ) ;
39
+ } ) ;
40
+ }
33
41
}
34
42
}
0 commit comments