Skip to content

Commit 29e1c53

Browse files
bamblackmhevery
authored andcommittedJul 24, 2019
feat(upgrade): support $element in upgraded component template/templateUrl functions (#31637)
PR Close #31637
1 parent 87ce4e9 commit 29e1c53

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed
 

‎packages/upgrade/src/common/src/upgrade_helper.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ export class UpgradeHelper {
7171
}
7272

7373
static getTemplate(
74-
$injector: IInjectorService, directive: IDirective, fetchRemoteTemplate = false): string
75-
|Promise<string> {
74+
$injector: IInjectorService, directive: IDirective, fetchRemoteTemplate = false,
75+
$element?: IAugmentedJQuery): string|Promise<string> {
7676
if (directive.template !== undefined) {
77-
return getOrCall<string>(directive.template);
77+
return getOrCall<string>(directive.template, $element);
7878
} else if (directive.templateUrl) {
7979
const $templateCache = $injector.get($TEMPLATE_CACHE) as ITemplateCacheService;
80-
const url = getOrCall<string>(directive.templateUrl);
80+
const url = getOrCall<string>(directive.templateUrl, $element);
8181
const template = $templateCache.get(url);
8282

8383
if (template !== undefined) {
@@ -114,7 +114,8 @@ export class UpgradeHelper {
114114

115115
compileTemplate(template?: string): ILinkFn {
116116
if (template === undefined) {
117-
template = UpgradeHelper.getTemplate(this.$injector, this.directive) as string;
117+
template =
118+
UpgradeHelper.getTemplate(this.$injector, this.directive, false, this.$element) as string;
118119
}
119120

120121
return this.compileHtml(template);
@@ -304,8 +305,8 @@ export class UpgradeHelper {
304305
}
305306
}
306307

307-
function getOrCall<T>(property: T | Function): T {
308-
return isFunction(property) ? property() : property;
308+
function getOrCall<T>(property: T | Function, ...args: any[]): T {
309+
return isFunction(property) ? property(...args) : property;
309310
}
310311

311312
// NOTE: Only works for `typeof T !== 'object'`.

‎packages/upgrade/static/test/integration/upgrade_component_spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ withEachNg1Version(() => {
106106
});
107107
}));
108108

109-
it('should support not pass any arguments to `template` function', async(() => {
109+
it('should pass $element to `template` function and not $attrs', async(() => {
110110
// Define `ng1Component`
111111
const ng1Component: angular.IComponent = {
112112
template: ($attrs: angular.IAttributes, $element: angular.IAugmentedJQuery) => {
113113
expect($attrs).toBeUndefined();
114-
expect($element).toBeUndefined();
114+
expect($element).toBeDefined();
115115

116116
return 'Hello, Angular!';
117117
}
@@ -241,12 +241,12 @@ withEachNg1Version(() => {
241241
});
242242
}));
243243

244-
it('should support not pass any arguments to `templateUrl` function', async(() => {
244+
it('should pass $element to `templateUrl` function and not $attrs', async(() => {
245245
// Define `ng1Component`
246246
const ng1Component: angular.IComponent = {
247247
templateUrl: ($attrs: angular.IAttributes, $element: angular.IAugmentedJQuery) => {
248248
expect($attrs).toBeUndefined();
249-
expect($element).toBeUndefined();
249+
expect($element).toBeDefined();
250250

251251
return 'ng1.component.html';
252252
}

0 commit comments

Comments
 (0)
Please sign in to comment.