From 29e1c53a31407633b6f10ec362f14db60cf524af Mon Sep 17 00:00:00 2001 From: Ben Black Date: Thu, 18 Jul 2019 16:48:28 -0400 Subject: [PATCH] feat(upgrade): support $element in upgraded component template/templateUrl functions (#31637) PR Close #31637 --- packages/upgrade/src/common/src/upgrade_helper.ts | 15 ++++++++------- .../test/integration/upgrade_component_spec.ts | 8 ++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/upgrade/src/common/src/upgrade_helper.ts b/packages/upgrade/src/common/src/upgrade_helper.ts index 04a02140fe..81813fcdad 100644 --- a/packages/upgrade/src/common/src/upgrade_helper.ts +++ b/packages/upgrade/src/common/src/upgrade_helper.ts @@ -71,13 +71,13 @@ export class UpgradeHelper { } static getTemplate( - $injector: IInjectorService, directive: IDirective, fetchRemoteTemplate = false): string - |Promise { + $injector: IInjectorService, directive: IDirective, fetchRemoteTemplate = false, + $element?: IAugmentedJQuery): string|Promise { if (directive.template !== undefined) { - return getOrCall(directive.template); + return getOrCall(directive.template, $element); } else if (directive.templateUrl) { const $templateCache = $injector.get($TEMPLATE_CACHE) as ITemplateCacheService; - const url = getOrCall(directive.templateUrl); + const url = getOrCall(directive.templateUrl, $element); const template = $templateCache.get(url); if (template !== undefined) { @@ -114,7 +114,8 @@ export class UpgradeHelper { compileTemplate(template?: string): ILinkFn { if (template === undefined) { - template = UpgradeHelper.getTemplate(this.$injector, this.directive) as string; + template = + UpgradeHelper.getTemplate(this.$injector, this.directive, false, this.$element) as string; } return this.compileHtml(template); @@ -304,8 +305,8 @@ export class UpgradeHelper { } } -function getOrCall(property: T | Function): T { - return isFunction(property) ? property() : property; +function getOrCall(property: T | Function, ...args: any[]): T { + return isFunction(property) ? property(...args) : property; } // NOTE: Only works for `typeof T !== 'object'`. diff --git a/packages/upgrade/static/test/integration/upgrade_component_spec.ts b/packages/upgrade/static/test/integration/upgrade_component_spec.ts index 3cd1a44596..9ef7b7dbbd 100644 --- a/packages/upgrade/static/test/integration/upgrade_component_spec.ts +++ b/packages/upgrade/static/test/integration/upgrade_component_spec.ts @@ -106,12 +106,12 @@ withEachNg1Version(() => { }); })); - it('should support not pass any arguments to `template` function', async(() => { + it('should pass $element to `template` function and not $attrs', async(() => { // Define `ng1Component` const ng1Component: angular.IComponent = { template: ($attrs: angular.IAttributes, $element: angular.IAugmentedJQuery) => { expect($attrs).toBeUndefined(); - expect($element).toBeUndefined(); + expect($element).toBeDefined(); return 'Hello, Angular!'; } @@ -241,12 +241,12 @@ withEachNg1Version(() => { }); })); - it('should support not pass any arguments to `templateUrl` function', async(() => { + it('should pass $element to `templateUrl` function and not $attrs', async(() => { // Define `ng1Component` const ng1Component: angular.IComponent = { templateUrl: ($attrs: angular.IAttributes, $element: angular.IAugmentedJQuery) => { expect($attrs).toBeUndefined(); - expect($element).toBeUndefined(); + expect($element).toBeDefined(); return 'ng1.component.html'; }