mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-28 15:28:15 +00:00
fix($compile): prevent duplicate directive controller instantiation
Closes #876
This commit is contained in:
parent
beea3a4bed
commit
843f762c57
2 changed files with 41 additions and 1 deletions
|
|
@ -819,7 +819,9 @@ function $CompileProvider($provide) {
|
||||||
originalWidgetNode = tElement[0],
|
originalWidgetNode = tElement[0],
|
||||||
asyncWidgetDirective = directives.shift(),
|
asyncWidgetDirective = directives.shift(),
|
||||||
// The fact that we have to copy and patch the directive seems wrong!
|
// The fact that we have to copy and patch the directive seems wrong!
|
||||||
syncWidgetDirective = extend({}, asyncWidgetDirective, {templateUrl:null, transclude:null}),
|
syncWidgetDirective = extend({}, asyncWidgetDirective, {
|
||||||
|
controller: null, templateUrl: null, transclude: null
|
||||||
|
}),
|
||||||
html = tElement.html();
|
html = tElement.html();
|
||||||
|
|
||||||
tElement.html('');
|
tElement.html('');
|
||||||
|
|
|
||||||
|
|
@ -1692,7 +1692,45 @@ describe('$compile', function() {
|
||||||
element = $compile('<div c1 c2><div dep></div></div>')($rootScope);
|
element = $compile('<div c1 c2><div dep></div></div>')($rootScope);
|
||||||
expect(log).toEqual('dep:c1-c2');
|
expect(log).toEqual('dep:c1-c2');
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should instantiate the controller just once when template/templateUrl', function() {
|
||||||
|
var syncCtrlSpy = jasmine.createSpy('sync controller'),
|
||||||
|
asyncCtrlSpy = jasmine.createSpy('async controller');
|
||||||
|
|
||||||
|
module(function($compileProvider) {
|
||||||
|
$compileProvider.directive('myDirectiveSync', valueFn({
|
||||||
|
template: '<div>Hello!</div>',
|
||||||
|
controller: syncCtrlSpy
|
||||||
|
}));
|
||||||
|
$compileProvider.directive('myDirectiveAsync', valueFn({
|
||||||
|
templateUrl: 'myDirectiveAsync.html',
|
||||||
|
controller: asyncCtrlSpy,
|
||||||
|
compile: function() {
|
||||||
|
return function() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
inject(function($templateCache, $compile, $rootScope) {
|
||||||
|
expect(syncCtrlSpy).not.toHaveBeenCalled();
|
||||||
|
expect(asyncCtrlSpy).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
$templateCache.put('myDirectiveAsync.html', '<div>Hello!</div>');
|
||||||
|
element = $compile('<div>'+
|
||||||
|
'<span xmy-directive-sync></span>' +
|
||||||
|
'<span my-directive-async></span>' +
|
||||||
|
'</div>')($rootScope);
|
||||||
|
expect(syncCtrlSpy).not.toHaveBeenCalled();
|
||||||
|
expect(asyncCtrlSpy).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
$rootScope.$apply();
|
||||||
|
|
||||||
|
//expect(syncCtrlSpy).toHaveBeenCalledOnce();
|
||||||
|
expect(asyncCtrlSpy).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue