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

Commit 843f762

Browse files
committed
fix($compile): prevent duplicate directive controller instantiation
Closes #876
1 parent beea3a4 commit 843f762

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/ng/compile.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,9 @@ function $CompileProvider($provide) {
819819
originalWidgetNode = tElement[0],
820820
asyncWidgetDirective = directives.shift(),
821821
// The fact that we have to copy and patch the directive seems wrong!
822-
syncWidgetDirective = extend({}, asyncWidgetDirective, {templateUrl:null, transclude:null}),
822+
syncWidgetDirective = extend({}, asyncWidgetDirective, {
823+
controller: null, templateUrl: null, transclude: null
824+
}),
823825
html = tElement.html();
824826

825827
tElement.html('');

test/ng/compileSpec.js

+38
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,45 @@ describe('$compile', function() {
16921692
element = $compile('<div c1 c2><div dep></div></div>')($rootScope);
16931693
expect(log).toEqual('dep:c1-c2');
16941694
});
1695+
});
1696+
16951697

1698+
it('should instantiate the controller just once when template/templateUrl', function() {
1699+
var syncCtrlSpy = jasmine.createSpy('sync controller'),
1700+
asyncCtrlSpy = jasmine.createSpy('async controller');
1701+
1702+
module(function($compileProvider) {
1703+
$compileProvider.directive('myDirectiveSync', valueFn({
1704+
template: '<div>Hello!</div>',
1705+
controller: syncCtrlSpy
1706+
}));
1707+
$compileProvider.directive('myDirectiveAsync', valueFn({
1708+
templateUrl: 'myDirectiveAsync.html',
1709+
controller: asyncCtrlSpy,
1710+
compile: function() {
1711+
return function() {
1712+
}
1713+
}
1714+
}));
1715+
});
1716+
1717+
inject(function($templateCache, $compile, $rootScope) {
1718+
expect(syncCtrlSpy).not.toHaveBeenCalled();
1719+
expect(asyncCtrlSpy).not.toHaveBeenCalled();
1720+
1721+
$templateCache.put('myDirectiveAsync.html', '<div>Hello!</div>');
1722+
element = $compile('<div>'+
1723+
'<span xmy-directive-sync></span>' +
1724+
'<span my-directive-async></span>' +
1725+
'</div>')($rootScope);
1726+
expect(syncCtrlSpy).not.toHaveBeenCalled();
1727+
expect(asyncCtrlSpy).not.toHaveBeenCalled();
1728+
1729+
$rootScope.$apply();
1730+
1731+
//expect(syncCtrlSpy).toHaveBeenCalledOnce();
1732+
expect(asyncCtrlSpy).toHaveBeenCalledOnce();
1733+
});
16961734
});
16971735
});
16981736

0 commit comments

Comments
 (0)