Skip to content

Instantly share code, notes, and snippets.

@yjaaidi
Created March 8, 2019 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yjaaidi/84c174fc03b25d8d4ef7978f5660c871 to your computer and use it in GitHub Desktop.
Save yjaaidi/84c174fc03b25d8d4ef7978f5660c871 to your computer and use it in GitHub Desktop.
Angular Tagged Template
/**
* @todo when `deps` will be added to `Component` definition,
* we could return `{deps, template}` then use it like this:
* ```
* @Component({
* selector: 'wt-root',
* ...ngTemplate`<${Child}></${Child}>`
* })
* ```
*/
export function ngTemplate(blockList, ...componentTypeList) {
const [firstBlock, ...remainingBlockList] = blockList;
return remainingBlockList.reduce((result, block, index) => {
const componentType = componentTypeList[index];
/* Grab the selector from component's metadata `ngComponentDef`. */
const selector = componentType.ngComponentDef.selectors[0][0];
return `${result}${selector}${block}`;
}, firstBlock);
}
@Component({
selector: 'wt-greetings',
template: `<h1>Hi {{ name }}</h1>`
})
export class Greetings {
@Input() name: string;
}
@Component({
selector: 'wt-root',
template: ngTemplate`
<${Greetings} name="foo"></${Greetings}>
<${Greetings} name="john"></${Greetings}>
`,
})
export class AppComponent {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment