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

Commit 7d0fe19

Browse files
pocesargkalpak
authored andcommitted
fix($compile): always use the DDO as this in pre-/post-linking functions
Closes #9306
1 parent 921f80e commit 7d0fe19

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

src/ng/compile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,9 +2342,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23422342
try {
23432343
linkFn = directive.compile($compileNode, templateAttrs, childTranscludeFn);
23442344
if (isFunction(linkFn)) {
2345-
addLinkFns(null, linkFn, attrStart, attrEnd);
2345+
addLinkFns(null, bind(directive, linkFn), attrStart, attrEnd);
23462346
} else if (linkFn) {
2347-
addLinkFns(linkFn.pre, linkFn.post, attrStart, attrEnd);
2347+
addLinkFns(bind(directive, linkFn.pre), bind(directive, linkFn.post), attrStart, attrEnd);
23482348
}
23492349
} catch (e) {
23502350
$exceptionHandler(e, startingTag($compileNode));

test/ng/compileSpec.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,96 @@ describe('$compile', function() {
225225
});
226226
inject(function($compile) {});
227227
});
228+
229+
it('should preserve context within declaration', function() {
230+
module(function() {
231+
directive('ff', function(log) {
232+
var declaration = {
233+
restrict: 'E',
234+
template: function(){
235+
log('ff template: ' + (this === declaration));
236+
},
237+
compile: function(){
238+
log('ff compile: ' + (this === declaration));
239+
return function(){
240+
log('ff post: ' + (this === declaration));
241+
};
242+
}
243+
};
244+
return declaration;
245+
});
246+
247+
directive('fff', function(log) {
248+
var declaration = {
249+
restrict: 'E',
250+
link: {
251+
pre: function(){
252+
log('fff pre: ' + (this === declaration));
253+
},
254+
post: function(){
255+
log('fff post: ' + (this === declaration));
256+
}
257+
}
258+
};
259+
return declaration;
260+
});
261+
262+
directive('ffff', function(log) {
263+
var declaration = {
264+
restrict: 'E',
265+
compile: function(){
266+
return {
267+
pre: function(){
268+
log('ffff pre: ' + (this === declaration));
269+
},
270+
post: function(){
271+
log('ffff post: ' + (this === declaration));
272+
}
273+
};
274+
}
275+
};
276+
return declaration;
277+
});
278+
279+
directive('fffff', function(log) {
280+
var declaration = {
281+
restrict: 'E',
282+
templateUrl: function(){
283+
log('fffff: ' + (this === declaration));
284+
}
285+
};
286+
return declaration;
287+
});
288+
289+
directive('ffffff', function(log) {
290+
var declaration = {
291+
restrict: 'E',
292+
link: function(){
293+
log('ffffff: ' + (this === declaration));
294+
}
295+
};
296+
return declaration;
297+
});
298+
});
299+
inject(function($compile, $rootScope, log) {
300+
$compile('<ff></ff>')($rootScope);
301+
$compile('<fff></fff>')($rootScope);
302+
$compile('<ffff></ffff>')($rootScope);
303+
$compile('<fffff></fffff>')($rootScope);
304+
$compile('<ffffff></ffffff>')($rootScope);
305+
expect(log).toEqual(
306+
'ff template: true; '+
307+
'ff compile: true; '+
308+
'ff post: true; '+
309+
'fff pre: true; '+
310+
'fff post: true; '+
311+
'ffff pre: true; '+
312+
'ffff post: true; '+
313+
'fffff: true; '+
314+
'ffffff: true'
315+
);
316+
});
317+
});
228318
});
229319

230320

0 commit comments

Comments
 (0)