diff --git a/src/ng/compile.js b/src/ng/compile.js index 393ab6375a27..cf60862b029b 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -2027,8 +2027,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn, thisLinkFn) { - var i, ii, linkFn, controller, isolateScope, elementControllers, transcludeFn, $element, - attrs; + var i, ii, linkFn, controller, isolateScope, parentScope, elementControllers, transcludeFn, + $element, attrs; if (compileNode === linkNode) { attrs = templateAttrs; @@ -2042,6 +2042,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { isolateScope = scope.$new(true); } + parentScope = (newScopeDirective && !newIsolateScopeDirective) ? scope.$parent : scope; + if (boundTranscludeFn) { // track `boundTranscludeFn` so it can be unwrapped if `transcludeFn` // is later passed as `parentBoundTranscludeFn` to `publicLinkFn` @@ -2060,7 +2062,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { compile.$$addScopeClass($element, true); isolateScope.$$isolateBindings = newIsolateScopeDirective.$$isolateBindings; - initializeDirectiveBindings(scope, attrs, isolateScope, + initializeDirectiveBindings(parentScope, attrs, isolateScope, isolateScope.$$isolateBindings, newIsolateScopeDirective, isolateScope); } @@ -2076,7 +2078,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { if (controller && controller.identifier && bindings) { controllerForBindings = controller; thisLinkFn.$$destroyBindings = - initializeDirectiveBindings(scope, attrs, controller.instance, + initializeDirectiveBindings(parentScope, attrs, controller.instance, bindings, scopeDirective); } } @@ -2093,7 +2095,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { // Remove and re-install bindToController bindings thisLinkFn.$$destroyBindings(); thisLinkFn.$$destroyBindings = - initializeDirectiveBindings(scope, attrs, controllerResult, bindings, scopeDirective); + initializeDirectiveBindings(parentScope, attrs, controllerResult, bindings, scopeDirective); } } } diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 767600571601..a09832a7d1cf 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -4406,6 +4406,66 @@ describe('$compile', function() { }); + it('should evaluate against the correct scope, when using `bindToController` (new scope)', + function() { + module(function($compileProvider, $controllerProvider) { + $controllerProvider.register({ + 'ParentCtrl': function() { + this.value1 = 'parent1'; + this.value2 = 'parent2'; + this.value3 = function() { return 'parent3'; }; + }, + 'ChildCtrl': function() { + this.value1 = 'child1'; + this.value2 = 'child2'; + this.value3 = function() { return 'child3'; }; + } + }); + + $compileProvider.directive('child', valueFn({ + scope: true, + controller: 'ChildCtrl', + controllerAs: 'ctrl', + bindToController: { + fromView1: '@', + fromView2: '=', + fromView3: '&' + }, + template: '' + })); + }); + + inject(function($compile, $rootScope) { + element = $compile( + '