Difference between ElemenetRef and TemplateRef in angular4












3















i have seen many examples of ElemenetRef and TemplateRef which got me more confused.




  1. what is the difference between ElementRef and TemplateRef why we should use one over another


HTML



<ng-template #element>
<div style="border:solid 3px yellow;width:250px;
height:250px;position:relative;top:0px;">
this is my element
</div>

</ng-template>
<ng-container #template>

</ng-container>


.ts file



@ViewChild('element', { read: TemplateRef }) element: TemplateRef<any>;
@ViewChild('template', { read: ViewContainerRef }) template: ViewContainerRef;

ngAfterViewInit() {
this.template.createEmbeddedView(this.element);
}


now if i change the above code to use ElementRef then also it works fine



@ViewChild('element', { read: ElementRef }) element: ElementRef;


so my question is why should i use TemplateRef if i can acheive the same with the use of ElementRef










share|improve this question


















  • 2





    ElementRef refers to an element of the DOM, whereas TemplateRef represents an embedded template (usually a component template). So to summarize, the template ref can contain several element refs, but the element ref can not contain a template ref.

    – trichetriche
    Nov 19 '18 at 12:28











  • so if we have a simple dom like this<span><span>then we have to use elementRef and if we have a template like this <div><div><div>something here</div></div></div> then we have to use templateRef, right? if yes, then what can a templateRef do which a elementRef cannot, can u please give me a brief idea, i am not able to find anythign online regarding this

    – Lijin Durairaj
    Nov 19 '18 at 12:37








  • 1





    in HTML it gives <app-my-component #templateRef><div #elementRef>Some content</div></app-my-component>. The template reference is to be used only with view container references, most of the time in structural directives (directives like *ngFor or *ngIf that manipulate the DOM directly). The documentation about ElementRef, TemplateRef and structural directives mght help you.

    – trichetriche
    Nov 19 '18 at 12:42











  • now viewContainerRef also has a createEmbeddedView() method same as the templateRef. then why should i use templateRef when i can use viewContainerRef and get the element using elementRef

    – Lijin Durairaj
    Nov 19 '18 at 12:49











  • You're getting away from your original question here. Don't make it an XY problem : tell me what you are trying to achieve, I'll tell you how to do it.

    – trichetriche
    Nov 19 '18 at 12:50
















3















i have seen many examples of ElemenetRef and TemplateRef which got me more confused.




  1. what is the difference between ElementRef and TemplateRef why we should use one over another


HTML



<ng-template #element>
<div style="border:solid 3px yellow;width:250px;
height:250px;position:relative;top:0px;">
this is my element
</div>

</ng-template>
<ng-container #template>

</ng-container>


.ts file



@ViewChild('element', { read: TemplateRef }) element: TemplateRef<any>;
@ViewChild('template', { read: ViewContainerRef }) template: ViewContainerRef;

ngAfterViewInit() {
this.template.createEmbeddedView(this.element);
}


now if i change the above code to use ElementRef then also it works fine



@ViewChild('element', { read: ElementRef }) element: ElementRef;


so my question is why should i use TemplateRef if i can acheive the same with the use of ElementRef










share|improve this question


















  • 2





    ElementRef refers to an element of the DOM, whereas TemplateRef represents an embedded template (usually a component template). So to summarize, the template ref can contain several element refs, but the element ref can not contain a template ref.

    – trichetriche
    Nov 19 '18 at 12:28











  • so if we have a simple dom like this<span><span>then we have to use elementRef and if we have a template like this <div><div><div>something here</div></div></div> then we have to use templateRef, right? if yes, then what can a templateRef do which a elementRef cannot, can u please give me a brief idea, i am not able to find anythign online regarding this

    – Lijin Durairaj
    Nov 19 '18 at 12:37








  • 1





    in HTML it gives <app-my-component #templateRef><div #elementRef>Some content</div></app-my-component>. The template reference is to be used only with view container references, most of the time in structural directives (directives like *ngFor or *ngIf that manipulate the DOM directly). The documentation about ElementRef, TemplateRef and structural directives mght help you.

    – trichetriche
    Nov 19 '18 at 12:42











  • now viewContainerRef also has a createEmbeddedView() method same as the templateRef. then why should i use templateRef when i can use viewContainerRef and get the element using elementRef

    – Lijin Durairaj
    Nov 19 '18 at 12:49











  • You're getting away from your original question here. Don't make it an XY problem : tell me what you are trying to achieve, I'll tell you how to do it.

    – trichetriche
    Nov 19 '18 at 12:50














3












3








3


1






i have seen many examples of ElemenetRef and TemplateRef which got me more confused.




  1. what is the difference between ElementRef and TemplateRef why we should use one over another


HTML



<ng-template #element>
<div style="border:solid 3px yellow;width:250px;
height:250px;position:relative;top:0px;">
this is my element
</div>

</ng-template>
<ng-container #template>

</ng-container>


.ts file



@ViewChild('element', { read: TemplateRef }) element: TemplateRef<any>;
@ViewChild('template', { read: ViewContainerRef }) template: ViewContainerRef;

ngAfterViewInit() {
this.template.createEmbeddedView(this.element);
}


now if i change the above code to use ElementRef then also it works fine



@ViewChild('element', { read: ElementRef }) element: ElementRef;


so my question is why should i use TemplateRef if i can acheive the same with the use of ElementRef










share|improve this question














i have seen many examples of ElemenetRef and TemplateRef which got me more confused.




  1. what is the difference between ElementRef and TemplateRef why we should use one over another


HTML



<ng-template #element>
<div style="border:solid 3px yellow;width:250px;
height:250px;position:relative;top:0px;">
this is my element
</div>

</ng-template>
<ng-container #template>

</ng-container>


.ts file



@ViewChild('element', { read: TemplateRef }) element: TemplateRef<any>;
@ViewChild('template', { read: ViewContainerRef }) template: ViewContainerRef;

ngAfterViewInit() {
this.template.createEmbeddedView(this.element);
}


now if i change the above code to use ElementRef then also it works fine



@ViewChild('element', { read: ElementRef }) element: ElementRef;


so my question is why should i use TemplateRef if i can acheive the same with the use of ElementRef







angular elementref






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 12:14









Lijin DurairajLijin Durairaj

1,00411834




1,00411834








  • 2





    ElementRef refers to an element of the DOM, whereas TemplateRef represents an embedded template (usually a component template). So to summarize, the template ref can contain several element refs, but the element ref can not contain a template ref.

    – trichetriche
    Nov 19 '18 at 12:28











  • so if we have a simple dom like this<span><span>then we have to use elementRef and if we have a template like this <div><div><div>something here</div></div></div> then we have to use templateRef, right? if yes, then what can a templateRef do which a elementRef cannot, can u please give me a brief idea, i am not able to find anythign online regarding this

    – Lijin Durairaj
    Nov 19 '18 at 12:37








  • 1





    in HTML it gives <app-my-component #templateRef><div #elementRef>Some content</div></app-my-component>. The template reference is to be used only with view container references, most of the time in structural directives (directives like *ngFor or *ngIf that manipulate the DOM directly). The documentation about ElementRef, TemplateRef and structural directives mght help you.

    – trichetriche
    Nov 19 '18 at 12:42











  • now viewContainerRef also has a createEmbeddedView() method same as the templateRef. then why should i use templateRef when i can use viewContainerRef and get the element using elementRef

    – Lijin Durairaj
    Nov 19 '18 at 12:49











  • You're getting away from your original question here. Don't make it an XY problem : tell me what you are trying to achieve, I'll tell you how to do it.

    – trichetriche
    Nov 19 '18 at 12:50














  • 2





    ElementRef refers to an element of the DOM, whereas TemplateRef represents an embedded template (usually a component template). So to summarize, the template ref can contain several element refs, but the element ref can not contain a template ref.

    – trichetriche
    Nov 19 '18 at 12:28











  • so if we have a simple dom like this<span><span>then we have to use elementRef and if we have a template like this <div><div><div>something here</div></div></div> then we have to use templateRef, right? if yes, then what can a templateRef do which a elementRef cannot, can u please give me a brief idea, i am not able to find anythign online regarding this

    – Lijin Durairaj
    Nov 19 '18 at 12:37








  • 1





    in HTML it gives <app-my-component #templateRef><div #elementRef>Some content</div></app-my-component>. The template reference is to be used only with view container references, most of the time in structural directives (directives like *ngFor or *ngIf that manipulate the DOM directly). The documentation about ElementRef, TemplateRef and structural directives mght help you.

    – trichetriche
    Nov 19 '18 at 12:42











  • now viewContainerRef also has a createEmbeddedView() method same as the templateRef. then why should i use templateRef when i can use viewContainerRef and get the element using elementRef

    – Lijin Durairaj
    Nov 19 '18 at 12:49











  • You're getting away from your original question here. Don't make it an XY problem : tell me what you are trying to achieve, I'll tell you how to do it.

    – trichetriche
    Nov 19 '18 at 12:50








2




2





ElementRef refers to an element of the DOM, whereas TemplateRef represents an embedded template (usually a component template). So to summarize, the template ref can contain several element refs, but the element ref can not contain a template ref.

– trichetriche
Nov 19 '18 at 12:28





ElementRef refers to an element of the DOM, whereas TemplateRef represents an embedded template (usually a component template). So to summarize, the template ref can contain several element refs, but the element ref can not contain a template ref.

– trichetriche
Nov 19 '18 at 12:28













so if we have a simple dom like this<span><span>then we have to use elementRef and if we have a template like this <div><div><div>something here</div></div></div> then we have to use templateRef, right? if yes, then what can a templateRef do which a elementRef cannot, can u please give me a brief idea, i am not able to find anythign online regarding this

– Lijin Durairaj
Nov 19 '18 at 12:37







so if we have a simple dom like this<span><span>then we have to use elementRef and if we have a template like this <div><div><div>something here</div></div></div> then we have to use templateRef, right? if yes, then what can a templateRef do which a elementRef cannot, can u please give me a brief idea, i am not able to find anythign online regarding this

– Lijin Durairaj
Nov 19 '18 at 12:37






1




1





in HTML it gives <app-my-component #templateRef><div #elementRef>Some content</div></app-my-component>. The template reference is to be used only with view container references, most of the time in structural directives (directives like *ngFor or *ngIf that manipulate the DOM directly). The documentation about ElementRef, TemplateRef and structural directives mght help you.

– trichetriche
Nov 19 '18 at 12:42





in HTML it gives <app-my-component #templateRef><div #elementRef>Some content</div></app-my-component>. The template reference is to be used only with view container references, most of the time in structural directives (directives like *ngFor or *ngIf that manipulate the DOM directly). The documentation about ElementRef, TemplateRef and structural directives mght help you.

– trichetriche
Nov 19 '18 at 12:42













now viewContainerRef also has a createEmbeddedView() method same as the templateRef. then why should i use templateRef when i can use viewContainerRef and get the element using elementRef

– Lijin Durairaj
Nov 19 '18 at 12:49





now viewContainerRef also has a createEmbeddedView() method same as the templateRef. then why should i use templateRef when i can use viewContainerRef and get the element using elementRef

– Lijin Durairaj
Nov 19 '18 at 12:49













You're getting away from your original question here. Don't make it an XY problem : tell me what you are trying to achieve, I'll tell you how to do it.

– trichetriche
Nov 19 '18 at 12:50





You're getting away from your original question here. Don't make it an XY problem : tell me what you are trying to achieve, I'll tell you how to do it.

– trichetriche
Nov 19 '18 at 12:50












1 Answer
1






active

oldest

votes


















1














ElementRef is simply a like document.getElementById('myId');



by ElementRef you can only able to do some decorations



TemplateRef is a embedded template where you can give it to angular to create Embedded View.



*ngFor doing the same, it reads the element as TemplateRef and injects mutiple times to create view with data



TemplateRef cannot be used as a element for css decorations in .ts






share|improve this answer
























  • @trichetriche, can u please command on this answer and let me know if it is correct, thanks

    – Lijin Durairaj
    Nov 19 '18 at 13:39











  • thanks for the help @Sheik Althaf

    – Lijin Durairaj
    Nov 19 '18 at 13:39











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374430%2fdifference-between-elemenetref-and-templateref-in-angular4%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














ElementRef is simply a like document.getElementById('myId');



by ElementRef you can only able to do some decorations



TemplateRef is a embedded template where you can give it to angular to create Embedded View.



*ngFor doing the same, it reads the element as TemplateRef and injects mutiple times to create view with data



TemplateRef cannot be used as a element for css decorations in .ts






share|improve this answer
























  • @trichetriche, can u please command on this answer and let me know if it is correct, thanks

    – Lijin Durairaj
    Nov 19 '18 at 13:39











  • thanks for the help @Sheik Althaf

    – Lijin Durairaj
    Nov 19 '18 at 13:39
















1














ElementRef is simply a like document.getElementById('myId');



by ElementRef you can only able to do some decorations



TemplateRef is a embedded template where you can give it to angular to create Embedded View.



*ngFor doing the same, it reads the element as TemplateRef and injects mutiple times to create view with data



TemplateRef cannot be used as a element for css decorations in .ts






share|improve this answer
























  • @trichetriche, can u please command on this answer and let me know if it is correct, thanks

    – Lijin Durairaj
    Nov 19 '18 at 13:39











  • thanks for the help @Sheik Althaf

    – Lijin Durairaj
    Nov 19 '18 at 13:39














1












1








1







ElementRef is simply a like document.getElementById('myId');



by ElementRef you can only able to do some decorations



TemplateRef is a embedded template where you can give it to angular to create Embedded View.



*ngFor doing the same, it reads the element as TemplateRef and injects mutiple times to create view with data



TemplateRef cannot be used as a element for css decorations in .ts






share|improve this answer













ElementRef is simply a like document.getElementById('myId');



by ElementRef you can only able to do some decorations



TemplateRef is a embedded template where you can give it to angular to create Embedded View.



*ngFor doing the same, it reads the element as TemplateRef and injects mutiple times to create view with data



TemplateRef cannot be used as a element for css decorations in .ts







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 13:16









Sheik AlthafSheik Althaf

27717




27717













  • @trichetriche, can u please command on this answer and let me know if it is correct, thanks

    – Lijin Durairaj
    Nov 19 '18 at 13:39











  • thanks for the help @Sheik Althaf

    – Lijin Durairaj
    Nov 19 '18 at 13:39



















  • @trichetriche, can u please command on this answer and let me know if it is correct, thanks

    – Lijin Durairaj
    Nov 19 '18 at 13:39











  • thanks for the help @Sheik Althaf

    – Lijin Durairaj
    Nov 19 '18 at 13:39

















@trichetriche, can u please command on this answer and let me know if it is correct, thanks

– Lijin Durairaj
Nov 19 '18 at 13:39





@trichetriche, can u please command on this answer and let me know if it is correct, thanks

– Lijin Durairaj
Nov 19 '18 at 13:39













thanks for the help @Sheik Althaf

– Lijin Durairaj
Nov 19 '18 at 13:39





thanks for the help @Sheik Althaf

– Lijin Durairaj
Nov 19 '18 at 13:39


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374430%2fdifference-between-elemenetref-and-templateref-in-angular4%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?