Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix($compile): evaluate against the correct scope with bindToController on new scope #13025

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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`
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
}
Expand Down
60 changes: 60 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<div ng-controller="ParentCtrl as ctrl">' +
'<child ' +
'from-view-1="{{ ctrl.value1 }}" ' +
'from-view-2="ctrl.value2" ' +
'from-view-3="ctrl.value3">' +
'</child>' +
'</div>')($rootScope);
$rootScope.$digest();

var parentCtrl = element.controller('ngController');
var childCtrl = element.find('child').controller('child');

expect(childCtrl.fromView1).toBe(parentCtrl.value1);
expect(childCtrl.fromView1).not.toBe(childCtrl.value1);
expect(childCtrl.fromView2).toBe(parentCtrl.value2);
expect(childCtrl.fromView2).not.toBe(childCtrl.value2);
expect(childCtrl.fromView3()()).toBe(parentCtrl.value3());
expect(childCtrl.fromView3()()).not.toBe(childCtrl.value3());

childCtrl.fromView2 = 'modified';
$rootScope.$digest();

expect(parentCtrl.value2).toBe('modified');
expect(childCtrl.value2).toBe('child2');
});
}
);


it('should put controller in scope when controller identifier present but not using controllerAs', function() {
var controllerCalled = false;
var myCtrl;
Expand Down