diff --git a/src/ng/compile.js b/src/ng/compile.js index 393ab6375a27..3e0b21068589 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1437,11 +1437,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { if (nodeLinkFn.scope) { childScope = scope.$new(); compile.$$addScopeInfo(jqLite(node), childScope); - var destroyBindings = nodeLinkFn.$$destroyBindings; - if (destroyBindings) { - nodeLinkFn.$$destroyBindings = null; - childScope.$on('$destroyed', destroyBindings); - } } else { childScope = scope; } @@ -1460,8 +1455,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { childBoundTranscludeFn = null; } - nodeLinkFn(childLinkFn, childScope, node, $rootElement, childBoundTranscludeFn, - nodeLinkFn); + nodeLinkFn(childLinkFn, childScope, node, $rootElement, childBoundTranscludeFn); } else if (childLinkFn) { childLinkFn(scope, node.childNodes, undefined, parentBoundTranscludeFn); @@ -2025,8 +2019,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { return elementControllers; } - function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn, - thisLinkFn) { + function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) { var i, ii, linkFn, controller, isolateScope, elementControllers, transcludeFn, $element, attrs; @@ -2060,24 +2053,28 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { compile.$$addScopeClass($element, true); isolateScope.$$isolateBindings = newIsolateScopeDirective.$$isolateBindings; - initializeDirectiveBindings(scope, attrs, isolateScope, - isolateScope.$$isolateBindings, - newIsolateScopeDirective, isolateScope); + var parentWatchDestroyer = initializeDirectiveBindings(scope, attrs, isolateScope, + isolateScope.$$isolateBindings, + newIsolateScopeDirective); + if (parentWatchDestroyer) { + isolateScope.$on('$destroy', parentWatchDestroyer); + } } if (elementControllers) { // Initialize bindToController bindings for new/isolate scopes var scopeDirective = newIsolateScopeDirective || newScopeDirective; var bindings; var controllerForBindings; + var destroyBindings; if (scopeDirective && elementControllers[scopeDirective.name]) { bindings = scopeDirective.$$bindings.bindToController; controller = elementControllers[scopeDirective.name]; if (controller && controller.identifier && bindings) { controllerForBindings = controller; - thisLinkFn.$$destroyBindings = + destroyBindings = initializeDirectiveBindings(scope, attrs, controller.instance, - bindings, scopeDirective); + bindings, scopeDirective) || noop; } } for (i in elementControllers) { @@ -2091,8 +2088,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { $element.data('$' + i + 'Controller', controllerResult); if (controller === controllerForBindings) { // Remove and re-install bindToController bindings - thisLinkFn.$$destroyBindings(); - thisLinkFn.$$destroyBindings = + destroyBindings(); + destroyBindings = initializeDirectiveBindings(scope, attrs, controllerResult, bindings, scopeDirective); } } @@ -2354,7 +2351,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { childBoundTranscludeFn = boundTranscludeFn; } afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, linkNode, $rootElement, - childBoundTranscludeFn, afterTemplateNodeLinkFn); + childBoundTranscludeFn); } linkQueue = null; }); @@ -2371,8 +2368,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { if (afterTemplateNodeLinkFn.transcludeOnThisElement) { childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude, boundTranscludeFn); } - afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, childBoundTranscludeFn, - afterTemplateNodeLinkFn); + afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, childBoundTranscludeFn); } }; } @@ -2632,8 +2628,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { // Set up $watches for isolate scope and controller bindings. This process // only occurs for isolate scopes and new scopes with controllerAs. - function initializeDirectiveBindings(scope, attrs, destination, bindings, - directive, newScope) { + function initializeDirectiveBindings(scope, attrs, destination, bindings, directive) { var onNewScopeDestroyed; forEach(bindings, function(definition, scopeName) { var attrName = definition.attrName, @@ -2719,16 +2714,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { break; } }); - var destroyBindings = onNewScopeDestroyed ? function destroyBindings() { + + return onNewScopeDestroyed && function destroyBindings() { for (var i = 0, ii = onNewScopeDestroyed.length; i < ii; ++i) { onNewScopeDestroyed[i](); } - } : noop; - if (newScope && destroyBindings !== noop) { - newScope.$on('$destroy', destroyBindings); - return noop; - } - return destroyBindings; + }; } }]; }