@@ -20,14 +20,53 @@ angular
20
20
* @description
21
21
* The `$mdSticky`service provides a mixin to make elements sticky.
22
22
*
23
+ * By default the `$mdSticky` service compiles the cloned element in the same scope as the actual element lives.
24
+ *
25
+ * <h3>Notes</h3>
26
+ * When using an element which is containing a compiled directive, which changed its DOM structure during compilation,
27
+ * you should compile the clone yourself using the plain template.<br/><br/>
28
+ * See the right usage below:
29
+ * <hljs lang="js">
30
+ * angular.module('myModule')
31
+ * .directive('stickySelect', function($mdSticky, $compile) {
32
+ * var SELECT_TEMPLATE =
33
+ * '<md-select ng-model="selected">' +
34
+ * '<md-option>Option 1</md-option>' +
35
+ * '</md-select>';
36
+ *
37
+ * return {
38
+ * restrict: 'E',
39
+ * replace: true,
40
+ * template: SELECT_TEMPLATE,
41
+ * link: function(scope,element) {
42
+ * $mdSticky(scope, element, $compile(SELECT_TEMPLATE)(scope));
43
+ * }
44
+ * };
45
+ * });
46
+ * </hljs>
47
+ *
48
+ * @usage
49
+ * <hljs lang="js">
50
+ * angular.module('myModule')
51
+ * .directive('stickyText', function($mdSticky, $compile) {
52
+ * return {
53
+ * restrict: 'E',
54
+ * template: '<span>Sticky Text</span>',
55
+ * link: function(scope,element) {
56
+ * $mdSticky(scope, element);
57
+ * }
58
+ * };
59
+ * });
60
+ * </hljs>
61
+ *
23
62
* @returns A `$mdSticky` function that takes three arguments:
24
63
* - `scope`
25
64
* - `element`: The element that will be 'sticky'
26
65
* - `elementClone`: A clone of the element, that will be shown
27
66
* when the user starts scrolling past the original element.
28
67
* If not provided, it will use the result of `element.clone()`.
29
68
*/
30
- function MdSticky ( $document , $mdConstant , $$rAF , $mdUtil ) {
69
+ function MdSticky ( $document , $mdConstant , $$rAF , $mdUtil , $compile ) {
31
70
32
71
var browserStickySupport = checkStickySupport ( ) ;
33
72
@@ -51,7 +90,11 @@ function MdSticky($document, $mdConstant, $$rAF, $mdUtil) {
51
90
contentCtrl . $element . data ( '$$sticky' , $$sticky ) ;
52
91
}
53
92
54
- var deregister = $$sticky . add ( element , stickyClone || element . clone ( ) ) ;
93
+ // Compile our clone element in the given scope if the stickyClone has no scope predefined.
94
+ var cloneElement = stickyClone && stickyClone . scope && stickyClone . scope ( ) ?
95
+ stickyClone : $compile ( stickyClone || element . clone ( ) ) ( scope ) ;
96
+
97
+ var deregister = $$sticky . add ( element , cloneElement ) ;
55
98
scope . $on ( '$destroy' , deregister ) ;
56
99
}
57
100
} ;
0 commit comments