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

fix(directives): fix this keyword inside link functions #9306

Closed
wants to merge 2 commits into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1541,9 +1541,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
try {
linkFn = directive.compile($compileNode, templateAttrs, childTranscludeFn);
if (isFunction(linkFn)) {
addLinkFns(null, linkFn, attrStart, attrEnd);
addLinkFns(null, bind(directive, linkFn), attrStart, attrEnd);
} else if (linkFn) {
addLinkFns(linkFn.pre, linkFn.post, attrStart, attrEnd);
addLinkFns(bind(directive, linkFn.pre), bind(directive, linkFn.post), attrStart, attrEnd);
}
} catch (e) {
$exceptionHandler(e, startingTag($compileNode));
Expand Down
90 changes: 90 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,96 @@ describe('$compile', function() {
});
inject(function($compile) {});
});

it('should preserve context within declaration', function() {
module(function() {
directive('ff', function(log) {
var declaration = {
restrict: 'E',
template: function(){
log('ff template: ' + (this === declaration));
},
compile: function(){
log('ff compile: ' + (this === declaration));
return function(){
log('ff post: ' + (this === declaration));
};
}
};
return declaration;
});

directive('fff', function(log) {
var declaration = {
restrict: 'E',
link: {
pre: function(){
log('fff pre: ' + (this === declaration));
},
post: function(){
log('fff post: ' + (this === declaration));
}
}
};
return declaration;
});

directive('ffff', function(log) {
var declaration = {
restrict: 'E',
compile: function(){
return {
pre: function(){
log('ffff pre: ' + (this === declaration));
},
post: function(){
log('ffff post: ' + (this === declaration));
}
};
}
};
return declaration;
});

directive('fffff', function(log) {
var declaration = {
restrict: 'E',
templateUrl: function(){
log('fffff: ' + (this === declaration));
}
};
return declaration;
});

directive('ffffff', function(log) {
var declaration = {
restrict: 'E',
link: function(){
log('ffffff: ' + (this === declaration));
}
};
return declaration;
});
});
inject(function($compile, $rootScope, log) {
$compile('<ff></ff>')($rootScope);
$compile('<fff></fff>')($rootScope);
$compile('<ffff></ffff>')($rootScope);
$compile('<fffff></fffff>')($rootScope);
$compile('<ffffff></ffffff>')($rootScope);
expect(log).toEqual(
'ff template: true; '+
'ff compile: true; '+
'ff post: true; '+
'fff pre: true; '+
'fff post: true; '+
'ffff pre: true; '+
'ffff post: true; '+
'fffff: true; '+
'ffffff: true'
);
});
});
});


Expand Down