Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(upgrade): support $element in upgraded component template/templateUrl functions #31637

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/upgrade/src/common/src/upgrade_helper.ts
Expand Up @@ -71,13 +71,13 @@ export class UpgradeHelper {
}

static getTemplate(
$injector: IInjectorService, directive: IDirective, fetchRemoteTemplate = false): string
|Promise<string> {
$injector: IInjectorService, directive: IDirective, fetchRemoteTemplate = false,
$element?: IAugmentedJQuery): string|Promise<string> {
if (directive.template !== undefined) {
return getOrCall<string>(directive.template);
return getOrCall<string>(directive.template, $element);
} else if (directive.templateUrl) {
const $templateCache = $injector.get($TEMPLATE_CACHE) as ITemplateCacheService;
const url = getOrCall<string>(directive.templateUrl);
const url = getOrCall<string>(directive.templateUrl, $element);
const template = $templateCache.get(url);

if (template !== undefined) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -304,8 +305,8 @@ export class UpgradeHelper {
}
}

function getOrCall<T>(property: T | Function): T {
return isFunction(property) ? property() : property;
function getOrCall<T>(property: T | Function, ...args: any[]): T {
return isFunction(property) ? property(...args) : property;
}

// NOTE: Only works for `typeof T !== 'object'`.
Expand Down
Expand Up @@ -106,12 +106,12 @@ withEachNg1Version(() => {
});
}));

it('should support not pass any arguments to `template` function', async(() => {
gkalpak marked this conversation as resolved.
Show resolved Hide resolved
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!';
}
Expand Down Expand Up @@ -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';
}
Expand Down