Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 026c87b

Browse files
devversionThomasBurleson
authored andcommitted
fix(sticky): compile cloned element in the same scope
Currently never apply a scope to the cloned element, which will cause problems with directives, which have bindings in their template. Also added the documentation for a common use-case Closes #4979 Closes #7151
1 parent 8961dcb commit 026c87b

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/components/sticky/sticky.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,53 @@ angular
2020
* @description
2121
* The `$mdSticky`service provides a mixin to make elements sticky.
2222
*
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+
*
2362
* @returns A `$mdSticky` function that takes three arguments:
2463
* - `scope`
2564
* - `element`: The element that will be 'sticky'
2665
* - `elementClone`: A clone of the element, that will be shown
2766
* when the user starts scrolling past the original element.
2867
* If not provided, it will use the result of `element.clone()`.
2968
*/
30-
function MdSticky($document, $mdConstant, $$rAF, $mdUtil) {
69+
function MdSticky($document, $mdConstant, $$rAF, $mdUtil, $compile) {
3170

3271
var browserStickySupport = checkStickySupport();
3372

@@ -51,7 +90,11 @@ function MdSticky($document, $mdConstant, $$rAF, $mdUtil) {
5190
contentCtrl.$element.data('$$sticky', $$sticky);
5291
}
5392

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);
5598
scope.$on('$destroy', deregister);
5699
}
57100
};

0 commit comments

Comments
 (0)