Created
March 8, 2019 14:37
-
-
Save yjaaidi/84c174fc03b25d8d4ef7978f5660c871 to your computer and use it in GitHub Desktop.
Angular Tagged Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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