Angular component with template outlet
I have a question component that that multiple implmentations but contains a common title component.
My desire is to create a component such that the template will output the correct type of question given the question model.
I have seen the idea of an ngTemplateOutlet however documentation on how this works is hard to understand (maybe not for you but it is for me) and I'm not sure by looking at the examples how I can implement this. I don't think I have to re-invent the wheel where, this seems like a common requirement.
My previous attempts include an [ngSwitch] container with many different *ngSwitchCases, however it would be ideal if I could define the templates externally and have the questionComponent aware of the mapping from questionType to available ng-templates.
e.g.
export class question
constructor(public title: string, public questionType: string, public answer: any)
@Component(
selector: 'app-question',
template: `
<div class="question">
<p>q.title</p>
<ng-container [ngTemplateOutlet]="theRightTemplate">
</ng-container>
</div>
`)
export class QuestionComponent
@Input()
question: Question;
I'm currently on Angular 7
Can someone please help me figure out how this would be achieved?
add a comment |
I have a question component that that multiple implmentations but contains a common title component.
My desire is to create a component such that the template will output the correct type of question given the question model.
I have seen the idea of an ngTemplateOutlet however documentation on how this works is hard to understand (maybe not for you but it is for me) and I'm not sure by looking at the examples how I can implement this. I don't think I have to re-invent the wheel where, this seems like a common requirement.
My previous attempts include an [ngSwitch] container with many different *ngSwitchCases, however it would be ideal if I could define the templates externally and have the questionComponent aware of the mapping from questionType to available ng-templates.
e.g.
export class question
constructor(public title: string, public questionType: string, public answer: any)
@Component(
selector: 'app-question',
template: `
<div class="question">
<p>q.title</p>
<ng-container [ngTemplateOutlet]="theRightTemplate">
</ng-container>
</div>
`)
export class QuestionComponent
@Input()
question: Question;
I'm currently on Angular 7
Can someone please help me figure out how this would be achieved?
add a comment |
I have a question component that that multiple implmentations but contains a common title component.
My desire is to create a component such that the template will output the correct type of question given the question model.
I have seen the idea of an ngTemplateOutlet however documentation on how this works is hard to understand (maybe not for you but it is for me) and I'm not sure by looking at the examples how I can implement this. I don't think I have to re-invent the wheel where, this seems like a common requirement.
My previous attempts include an [ngSwitch] container with many different *ngSwitchCases, however it would be ideal if I could define the templates externally and have the questionComponent aware of the mapping from questionType to available ng-templates.
e.g.
export class question
constructor(public title: string, public questionType: string, public answer: any)
@Component(
selector: 'app-question',
template: `
<div class="question">
<p>q.title</p>
<ng-container [ngTemplateOutlet]="theRightTemplate">
</ng-container>
</div>
`)
export class QuestionComponent
@Input()
question: Question;
I'm currently on Angular 7
Can someone please help me figure out how this would be achieved?
I have a question component that that multiple implmentations but contains a common title component.
My desire is to create a component such that the template will output the correct type of question given the question model.
I have seen the idea of an ngTemplateOutlet however documentation on how this works is hard to understand (maybe not for you but it is for me) and I'm not sure by looking at the examples how I can implement this. I don't think I have to re-invent the wheel where, this seems like a common requirement.
My previous attempts include an [ngSwitch] container with many different *ngSwitchCases, however it would be ideal if I could define the templates externally and have the questionComponent aware of the mapping from questionType to available ng-templates.
e.g.
export class question
constructor(public title: string, public questionType: string, public answer: any)
@Component(
selector: 'app-question',
template: `
<div class="question">
<p>q.title</p>
<ng-container [ngTemplateOutlet]="theRightTemplate">
</ng-container>
</div>
`)
export class QuestionComponent
@Input()
question: Question;
I'm currently on Angular 7
Can someone please help me figure out how this would be achieved?
asked Nov 15 '18 at 3:16
coderatchetcoderatchet
3,78784388
3,78784388
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
ok, i will give u an example and explain to it:
link :https://github.com/100cm/at-ui-angular/blob/master/src/app/components/switch/switch.component.ts
For your code, we must define an @Input variable
export class question
constructor(public title: string, public questionType: string, public answer: any)
@Component(
selector: 'app-question',
template: `
<div class="question">
<p>q.title</p>
<ng-container [ngTemplateOutlet]="theRightTemplate">
</ng-container>
</div>
`)
export class QuestionComponent
@Input()
question: Question;
@Input()
myTemplate:TemplateRef
then change the <ng-container> to <ng-template> (I don't think <ng-container> can used to template outlet
<ng-template [ngTemplateOutlet]="myTemplate">
Ok ,then use your <app-question> now !
what_ever_html:
<app-question [myTemplate]="my_template"></app-question>
<ng-template #my_template> i am worked now !</ng-template>
Hope this could help u !
add a comment |
You can use @ContentChild('tableHeader') header: TemplateRef<any>; in your QuestionComponent You can use ContentChild to get the first element or the directive matching the selector from the content DOM.
If the content DOM changes, and a new child matches the selector, the property will be updated.
Your child component.html reads as
<ng-template [ngTemplateOutlet]="header"></ng-template>
<ng-template [ngTemplateOutlet]="body"></ng-template>
Where [ngTemplateOutlet] will read the property value and paste the content in the specified template
Finally when you refer the content from the parent component it will get placed with the template mentioned above
Method to pass the content from parent to child
<app-question>
<ng-template #tableHeader></ng-template>
<ng-template #body></ng-template>
</app-question>
Any content inside this <ng-template> will be placed inside the child component
In child component you can add as many templates you can where all the templates will be placed with the content passed form the parent component - This is another way to pass content from parent to child - hope this helps - Thanks Happy coding !!
click here for Reference
add a comment |
There are two important things when it comes to making the dynamic component by using the template.
Template- First one is template itself which needs to be injected and important point to be noted here is that it require thecontextwhich carries additional information, In your casequestiondetails.ngTemplateOutlet- where your template needs to be injected. It accept thetemplateandcontextfor the template.
Here is the simple example -
import Component from '@angular/core';
@Component(
selector: 'my-app',
template: `
<ng-container *ngTemplateOutlet="templateRef; context: entry: entry"></ng-container>
<ng-template #templateRef let-entry="entry">My entry is entry </ng-template>
`,
)
export class AppComponent
entry =
label: 'This is a label',
description: 'Desc',
Example 1- https://stackblitz.com/edit/ng-template-outlet-6pzddz
Example 2- https://stackblitz.com/edit/angular-reusable-components-ngtemplateoutlet-5nidmd
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311892%2fangular-component-with-template-outlet%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
ok, i will give u an example and explain to it:
link :https://github.com/100cm/at-ui-angular/blob/master/src/app/components/switch/switch.component.ts
For your code, we must define an @Input variable
export class question
constructor(public title: string, public questionType: string, public answer: any)
@Component(
selector: 'app-question',
template: `
<div class="question">
<p>q.title</p>
<ng-container [ngTemplateOutlet]="theRightTemplate">
</ng-container>
</div>
`)
export class QuestionComponent
@Input()
question: Question;
@Input()
myTemplate:TemplateRef
then change the <ng-container> to <ng-template> (I don't think <ng-container> can used to template outlet
<ng-template [ngTemplateOutlet]="myTemplate">
Ok ,then use your <app-question> now !
what_ever_html:
<app-question [myTemplate]="my_template"></app-question>
<ng-template #my_template> i am worked now !</ng-template>
Hope this could help u !
add a comment |
ok, i will give u an example and explain to it:
link :https://github.com/100cm/at-ui-angular/blob/master/src/app/components/switch/switch.component.ts
For your code, we must define an @Input variable
export class question
constructor(public title: string, public questionType: string, public answer: any)
@Component(
selector: 'app-question',
template: `
<div class="question">
<p>q.title</p>
<ng-container [ngTemplateOutlet]="theRightTemplate">
</ng-container>
</div>
`)
export class QuestionComponent
@Input()
question: Question;
@Input()
myTemplate:TemplateRef
then change the <ng-container> to <ng-template> (I don't think <ng-container> can used to template outlet
<ng-template [ngTemplateOutlet]="myTemplate">
Ok ,then use your <app-question> now !
what_ever_html:
<app-question [myTemplate]="my_template"></app-question>
<ng-template #my_template> i am worked now !</ng-template>
Hope this could help u !
add a comment |
ok, i will give u an example and explain to it:
link :https://github.com/100cm/at-ui-angular/blob/master/src/app/components/switch/switch.component.ts
For your code, we must define an @Input variable
export class question
constructor(public title: string, public questionType: string, public answer: any)
@Component(
selector: 'app-question',
template: `
<div class="question">
<p>q.title</p>
<ng-container [ngTemplateOutlet]="theRightTemplate">
</ng-container>
</div>
`)
export class QuestionComponent
@Input()
question: Question;
@Input()
myTemplate:TemplateRef
then change the <ng-container> to <ng-template> (I don't think <ng-container> can used to template outlet
<ng-template [ngTemplateOutlet]="myTemplate">
Ok ,then use your <app-question> now !
what_ever_html:
<app-question [myTemplate]="my_template"></app-question>
<ng-template #my_template> i am worked now !</ng-template>
Hope this could help u !
ok, i will give u an example and explain to it:
link :https://github.com/100cm/at-ui-angular/blob/master/src/app/components/switch/switch.component.ts
For your code, we must define an @Input variable
export class question
constructor(public title: string, public questionType: string, public answer: any)
@Component(
selector: 'app-question',
template: `
<div class="question">
<p>q.title</p>
<ng-container [ngTemplateOutlet]="theRightTemplate">
</ng-container>
</div>
`)
export class QuestionComponent
@Input()
question: Question;
@Input()
myTemplate:TemplateRef
then change the <ng-container> to <ng-template> (I don't think <ng-container> can used to template outlet
<ng-template [ngTemplateOutlet]="myTemplate">
Ok ,then use your <app-question> now !
what_ever_html:
<app-question [myTemplate]="my_template"></app-question>
<ng-template #my_template> i am worked now !</ng-template>
Hope this could help u !
answered Nov 15 '18 at 3:28
junkjunk
270518
270518
add a comment |
add a comment |
You can use @ContentChild('tableHeader') header: TemplateRef<any>; in your QuestionComponent You can use ContentChild to get the first element or the directive matching the selector from the content DOM.
If the content DOM changes, and a new child matches the selector, the property will be updated.
Your child component.html reads as
<ng-template [ngTemplateOutlet]="header"></ng-template>
<ng-template [ngTemplateOutlet]="body"></ng-template>
Where [ngTemplateOutlet] will read the property value and paste the content in the specified template
Finally when you refer the content from the parent component it will get placed with the template mentioned above
Method to pass the content from parent to child
<app-question>
<ng-template #tableHeader></ng-template>
<ng-template #body></ng-template>
</app-question>
Any content inside this <ng-template> will be placed inside the child component
In child component you can add as many templates you can where all the templates will be placed with the content passed form the parent component - This is another way to pass content from parent to child - hope this helps - Thanks Happy coding !!
click here for Reference
add a comment |
You can use @ContentChild('tableHeader') header: TemplateRef<any>; in your QuestionComponent You can use ContentChild to get the first element or the directive matching the selector from the content DOM.
If the content DOM changes, and a new child matches the selector, the property will be updated.
Your child component.html reads as
<ng-template [ngTemplateOutlet]="header"></ng-template>
<ng-template [ngTemplateOutlet]="body"></ng-template>
Where [ngTemplateOutlet] will read the property value and paste the content in the specified template
Finally when you refer the content from the parent component it will get placed with the template mentioned above
Method to pass the content from parent to child
<app-question>
<ng-template #tableHeader></ng-template>
<ng-template #body></ng-template>
</app-question>
Any content inside this <ng-template> will be placed inside the child component
In child component you can add as many templates you can where all the templates will be placed with the content passed form the parent component - This is another way to pass content from parent to child - hope this helps - Thanks Happy coding !!
click here for Reference
add a comment |
You can use @ContentChild('tableHeader') header: TemplateRef<any>; in your QuestionComponent You can use ContentChild to get the first element or the directive matching the selector from the content DOM.
If the content DOM changes, and a new child matches the selector, the property will be updated.
Your child component.html reads as
<ng-template [ngTemplateOutlet]="header"></ng-template>
<ng-template [ngTemplateOutlet]="body"></ng-template>
Where [ngTemplateOutlet] will read the property value and paste the content in the specified template
Finally when you refer the content from the parent component it will get placed with the template mentioned above
Method to pass the content from parent to child
<app-question>
<ng-template #tableHeader></ng-template>
<ng-template #body></ng-template>
</app-question>
Any content inside this <ng-template> will be placed inside the child component
In child component you can add as many templates you can where all the templates will be placed with the content passed form the parent component - This is another way to pass content from parent to child - hope this helps - Thanks Happy coding !!
click here for Reference
You can use @ContentChild('tableHeader') header: TemplateRef<any>; in your QuestionComponent You can use ContentChild to get the first element or the directive matching the selector from the content DOM.
If the content DOM changes, and a new child matches the selector, the property will be updated.
Your child component.html reads as
<ng-template [ngTemplateOutlet]="header"></ng-template>
<ng-template [ngTemplateOutlet]="body"></ng-template>
Where [ngTemplateOutlet] will read the property value and paste the content in the specified template
Finally when you refer the content from the parent component it will get placed with the template mentioned above
Method to pass the content from parent to child
<app-question>
<ng-template #tableHeader></ng-template>
<ng-template #body></ng-template>
</app-question>
Any content inside this <ng-template> will be placed inside the child component
In child component you can add as many templates you can where all the templates will be placed with the content passed form the parent component - This is another way to pass content from parent to child - hope this helps - Thanks Happy coding !!
click here for Reference
answered Nov 15 '18 at 3:54
RahulRahul
1,1092318
1,1092318
add a comment |
add a comment |
There are two important things when it comes to making the dynamic component by using the template.
Template- First one is template itself which needs to be injected and important point to be noted here is that it require thecontextwhich carries additional information, In your casequestiondetails.ngTemplateOutlet- where your template needs to be injected. It accept thetemplateandcontextfor the template.
Here is the simple example -
import Component from '@angular/core';
@Component(
selector: 'my-app',
template: `
<ng-container *ngTemplateOutlet="templateRef; context: entry: entry"></ng-container>
<ng-template #templateRef let-entry="entry">My entry is entry </ng-template>
`,
)
export class AppComponent
entry =
label: 'This is a label',
description: 'Desc',
Example 1- https://stackblitz.com/edit/ng-template-outlet-6pzddz
Example 2- https://stackblitz.com/edit/angular-reusable-components-ngtemplateoutlet-5nidmd
add a comment |
There are two important things when it comes to making the dynamic component by using the template.
Template- First one is template itself which needs to be injected and important point to be noted here is that it require thecontextwhich carries additional information, In your casequestiondetails.ngTemplateOutlet- where your template needs to be injected. It accept thetemplateandcontextfor the template.
Here is the simple example -
import Component from '@angular/core';
@Component(
selector: 'my-app',
template: `
<ng-container *ngTemplateOutlet="templateRef; context: entry: entry"></ng-container>
<ng-template #templateRef let-entry="entry">My entry is entry </ng-template>
`,
)
export class AppComponent
entry =
label: 'This is a label',
description: 'Desc',
Example 1- https://stackblitz.com/edit/ng-template-outlet-6pzddz
Example 2- https://stackblitz.com/edit/angular-reusable-components-ngtemplateoutlet-5nidmd
add a comment |
There are two important things when it comes to making the dynamic component by using the template.
Template- First one is template itself which needs to be injected and important point to be noted here is that it require thecontextwhich carries additional information, In your casequestiondetails.ngTemplateOutlet- where your template needs to be injected. It accept thetemplateandcontextfor the template.
Here is the simple example -
import Component from '@angular/core';
@Component(
selector: 'my-app',
template: `
<ng-container *ngTemplateOutlet="templateRef; context: entry: entry"></ng-container>
<ng-template #templateRef let-entry="entry">My entry is entry </ng-template>
`,
)
export class AppComponent
entry =
label: 'This is a label',
description: 'Desc',
Example 1- https://stackblitz.com/edit/ng-template-outlet-6pzddz
Example 2- https://stackblitz.com/edit/angular-reusable-components-ngtemplateoutlet-5nidmd
There are two important things when it comes to making the dynamic component by using the template.
Template- First one is template itself which needs to be injected and important point to be noted here is that it require thecontextwhich carries additional information, In your casequestiondetails.ngTemplateOutlet- where your template needs to be injected. It accept thetemplateandcontextfor the template.
Here is the simple example -
import Component from '@angular/core';
@Component(
selector: 'my-app',
template: `
<ng-container *ngTemplateOutlet="templateRef; context: entry: entry"></ng-container>
<ng-template #templateRef let-entry="entry">My entry is entry </ng-template>
`,
)
export class AppComponent
entry =
label: 'This is a label',
description: 'Desc',
Example 1- https://stackblitz.com/edit/ng-template-outlet-6pzddz
Example 2- https://stackblitz.com/edit/angular-reusable-components-ngtemplateoutlet-5nidmd
edited Nov 15 '18 at 7:21
answered Nov 15 '18 at 3:51
Sunil SinghSunil Singh
6,3772628
6,3772628
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311892%2fangular-component-with-template-outlet%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown