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

Commit 2470582

Browse files
committed
fix($compile): properly bind context to linking functions for directives with templateUrl
Also, fix some styling issues.
1 parent 7d0fe19 commit 2470582

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

src/ng/compile.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,10 +2341,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23412341
} else if (directive.compile) {
23422342
try {
23432343
linkFn = directive.compile($compileNode, templateAttrs, childTranscludeFn);
2344+
var context = directive.$$originalDirective || directive;
23442345
if (isFunction(linkFn)) {
2345-
addLinkFns(null, bind(directive, linkFn), attrStart, attrEnd);
2346+
addLinkFns(null, bind(context, linkFn), attrStart, attrEnd);
23462347
} else if (linkFn) {
2347-
addLinkFns(bind(directive, linkFn.pre), bind(directive, linkFn.post), attrStart, attrEnd);
2348+
addLinkFns(bind(context, linkFn.pre), bind(context, linkFn.post), attrStart, attrEnd);
23482349
}
23492350
} catch (e) {
23502351
$exceptionHandler(e, startingTag($compileNode));

test/ng/compileSpec.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@ describe('$compile', function() {
231231
directive('ff', function(log) {
232232
var declaration = {
233233
restrict: 'E',
234-
template: function(){
234+
template: function() {
235235
log('ff template: ' + (this === declaration));
236236
},
237-
compile: function(){
237+
compile: function() {
238238
log('ff compile: ' + (this === declaration));
239-
return function(){
239+
return function() {
240240
log('ff post: ' + (this === declaration));
241241
};
242242
}
@@ -248,10 +248,10 @@ describe('$compile', function() {
248248
var declaration = {
249249
restrict: 'E',
250250
link: {
251-
pre: function(){
251+
pre: function() {
252252
log('fff pre: ' + (this === declaration));
253253
},
254-
post: function(){
254+
post: function() {
255255
log('fff post: ' + (this === declaration));
256256
}
257257
}
@@ -262,12 +262,12 @@ describe('$compile', function() {
262262
directive('ffff', function(log) {
263263
var declaration = {
264264
restrict: 'E',
265-
compile: function(){
265+
compile: function() {
266266
return {
267-
pre: function(){
267+
pre: function() {
268268
log('ffff pre: ' + (this === declaration));
269269
},
270-
post: function(){
270+
post: function() {
271271
log('ffff post: ' + (this === declaration));
272272
}
273273
};
@@ -279,39 +279,37 @@ describe('$compile', function() {
279279
directive('fffff', function(log) {
280280
var declaration = {
281281
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));
282+
templateUrl: function() {
283+
log('fffff templateUrl: ' + (this === declaration));
284+
return 'fffff.html';
285+
},
286+
link: function() {
287+
log('fffff post: ' + (this === declaration));
294288
}
295289
};
296290
return declaration;
297291
});
298292
});
299-
inject(function($compile, $rootScope, log) {
293+
294+
inject(function($compile, $rootScope, $templateCache, log) {
295+
$templateCache.put('fffff.html', '');
296+
300297
$compile('<ff></ff>')($rootScope);
301298
$compile('<fff></fff>')($rootScope);
302299
$compile('<ffff></ffff>')($rootScope);
303300
$compile('<fffff></fffff>')($rootScope);
304-
$compile('<ffffff></ffffff>')($rootScope);
301+
$rootScope.$digest();
302+
305303
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'
304+
'ff template: true; ' +
305+
'ff compile: true; ' +
306+
'ff post: true; ' +
307+
'fff pre: true; ' +
308+
'fff post: true; ' +
309+
'ffff pre: true; ' +
310+
'ffff post: true; ' +
311+
'fffff templateUrl: true; ' +
312+
'fffff post: true'
315313
);
316314
});
317315
});

0 commit comments

Comments
 (0)