|
24 | 24 | $scope.colContainer = containerCtrl.colContainer;
|
25 | 25 |
|
26 | 26 | // Function for attaching the template to this scope
|
| 27 | + var clonedElement, cloneScope; |
27 | 28 | function compileTemplate() {
|
28 | 29 | var compiledElementFn = $scope.row.compiledElementFn;
|
29 | 30 |
|
30 |
| - compiledElementFn($scope, function (clonedElement, scope) { |
31 |
| - $elm.empty().append(clonedElement); |
| 31 | + // Create a new scope for the contents of this row, so we can destroy it later if need be |
| 32 | + var newScope = $scope.$new(); |
| 33 | + |
| 34 | + compiledElementFn(newScope, function (newElm, scope) { |
| 35 | + // If we already have a cloned element, we need to remove it and destroy its scope |
| 36 | + if (clonedElement) { |
| 37 | + clonedElement.remove(); |
| 38 | + cloneScope.$destroy(); |
| 39 | + } |
| 40 | + |
| 41 | + // Empty the row and append the new element |
| 42 | + $elm.empty().append(newElm); |
| 43 | + |
| 44 | + // Save the new cloned element and scope |
| 45 | + clonedElement = newElm; |
| 46 | + cloneScope = newScope; |
32 | 47 | });
|
33 | 48 | }
|
34 | 49 |
|
|
38 | 53 | // If the row's compiled element function changes, we need to replace this element's contents with the new compiled template
|
39 | 54 | $scope.$watch('row.compiledElementFn', function (newFunc, oldFunc) {
|
40 | 55 | if (newFunc !== oldFunc) {
|
41 |
| - newFunc($scope, function (clonedElement, scope) { |
42 |
| - $elm.empty().append(clonedElement); |
43 |
| - }); |
| 56 | + compileTemplate(); |
44 | 57 | }
|
45 | 58 | });
|
46 | 59 | },
|
47 | 60 | post: function($scope, $elm, $attrs, controllers) {
|
48 | 61 | var uiGridCtrl = controllers[0];
|
49 | 62 | var containerCtrl = controllers[1];
|
50 | 63 |
|
51 |
| - //add optional reference to externalScopes function to scope |
52 |
| - //so it can be retrieved in lower elements |
| 64 | + // Sdd optional reference to externalScopes function to scope |
| 65 | + // so it can be retrieved in lower elements |
53 | 66 | $scope.getExternalScopes = uiGridCtrl.getExternalScopes;
|
54 | 67 | }
|
55 | 68 | };
|
|
0 commit comments