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

fix ($compile): keeps prototype properties for template URL directives #10926

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
3 changes: 1 addition & 2 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2155,8 +2155,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
afterTemplateChildLinkFn,
beforeTemplateCompileNode = $compileNode[0],
origAsyncDirective = directives.shift(),
// The fact that we have to copy and patch the directive seems wrong!
derivedSyncDirective = extend({}, origAsyncDirective, {
derivedSyncDirective = inherit(origAsyncDirective, {
templateUrl: null, transclude: null, replace: null, $$originalDirective: origAsyncDirective
}),
templateUrl = (isFunction(origAsyncDirective.templateUrl))
Expand Down
49 changes: 49 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,29 @@ describe('$compile', function() {
expect(element.find('p').text()).toBe('Hello, world!');
});
});

it('should keep prototype properties on directive', function() {
module(function() {
function DirectiveClass() {
this.restrict = 'E';
this.template = "<p>{{value}}</p>";
}

DirectiveClass.prototype.compile = function() {
return function(scope, element, attrs) {
scope.value = "Test Value";
};
};

directive('templateUrlWithPrototype', valueFn(new DirectiveClass()));
});

inject(function($compile, $rootScope) {
element = $compile('<template-url-with-prototype><template-url-with-prototype>')($rootScope);
$rootScope.$digest();
expect(element.find("p")[0].innerHTML).toEqual("Test Value");
});
});
});


Expand Down Expand Up @@ -2034,6 +2057,32 @@ describe('$compile', function() {
});
});

it('should keep prototype properties on sync version of async directive', function() {
module(function() {
function DirectiveClass() {
this.restrict = 'E';
this.templateUrl = "test.html";
}

DirectiveClass.prototype.compile = function() {
return function(scope, element, attrs) {
scope.value = "Test Value";
};
};

directive('templateUrlWithPrototype', valueFn(new DirectiveClass()));
});

inject(function($compile, $rootScope, $httpBackend) {
$httpBackend.whenGET('test.html').
respond('<p>{{value}}</p>');
element = $compile('<template-url-with-prototype><template-url-with-prototype>')($rootScope);
$httpBackend.flush();
$rootScope.$digest();
expect(element.find("p")[0].innerHTML).toEqual("Test Value");
});
});

});


Expand Down