diff --git a/src/ng/compile.js b/src/ng/compile.js index 2376257b5331..b333b079aa82 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -2084,7 +2084,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { } function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) { - var linkFn, isolateScope, elementControllers, transcludeFn, $element, + var linkFn, isolateScope, controllerScope, elementControllers, transcludeFn, $element, attrs, removeScopeBindingWatches, removeControllerBindingWatches; if (compileNode === linkNode) { @@ -2095,8 +2095,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { attrs = new Attributes($element, templateAttrs); } + controllerScope = scope; if (newIsolateScopeDirective) { isolateScope = scope.$new(true); + } else if (newScopeDirective) { + controllerScope = scope.$parent; } if (boundTranscludeFn) { @@ -2133,7 +2136,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { if (controller.identifier && bindings) { removeControllerBindingWatches = - initializeDirectiveBindings(scope, attrs, controller.instance, bindings, controllerDirective); + initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective); } var controllerResult = controller(); @@ -2144,7 +2147,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { $element.data('$' + controllerDirective.name + 'Controller', controllerResult); removeControllerBindingWatches && removeControllerBindingWatches(); removeControllerBindingWatches = - initializeDirectiveBindings(scope, attrs, controller.instance, bindings, controllerDirective); + initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective); } } diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 136bec90a25b..db48aa665770 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -4591,6 +4591,124 @@ 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 as ctrl', + bindToController: { + fromParent1: '@', + fromParent2: '=', + fromParent3: '&' + }, + template: '' + })); + }); + + inject(function($compile, $rootScope) { + element = $compile( + '