NgTemplateOutlet in Angular - Everything You Have to Know (2022)

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • This video will teach you how to utilize an Angular ngTemplateOutlet directive to create reusable components with customizable and extensible views. You will learn everything you have to know about this directive. We will cover all available features like creating views from templates, providing template context data, and controlling the proper Angular Dependency Injection flow. So if you watch the video until the end, you can be sure that you will be able to handle 80% of all possible use cases related to component view customization.
    💥 Become a PRO with my in-depth Angular Forms Course💥
    🔗 10% discount for the first 10 students - bit.ly/advanced-ng-forms-disc...
    🕒 Time Codes:
    00:00:00 - Intro;
    00:00:58 - Project Overview;
    00:03:17 - Describing the Problem;
    00:04:49 - How to create Embedded Views in Angular;
    00:10:54 - Let's meet [ngTemplateOutlet] directive;
    00:12:43 - Conditionally Rendering ng-templates;
    00:16:38 - Introducing Template Context;
    00:24:01 - Controlling Parent Injector for components in NgTemplates;
    00:30:09 - Recap things we learned;
    00:32:14 - Disadvantages of [ngTemplateOutlet] directive;
    00:34:00 - Outro;
    🔗 Project Code on GitHub:
    github.com/DMezhenskyi/angula... (initial state in main branch)
    🙌 Support Dmytro bit.ly/donate-to-decoded-fron...
    💡 Short Frontend Snacks (Tips) every week here:
    Twitter - / decodedfrontend
    Instagram - / decodedfrontend
    LinkedIn - / dmezhenskyi
    #ng #angular #angular15
  • Věda a technologie

Komentáře • 171

  • @DecodedFrontend
    @DecodedFrontend  Před rokem +19

    💥 Learn Angular Forms in-depth and start building complex form controls with ease💥
    🔗 10% discount for the first 10 students - bit.ly/advanced-ng-forms-discounted
    💡 Short Frontend Snacks (Tips) every week here:
    Twitter - twitter.com/DecodedFrontend
    Instagram - instagram.com/decodedfrontend
    LinkedIn - www.linkedin.com/in/dmezhenskyi

  • @ibrahimbhabay37
    @ibrahimbhabay37 Před rokem +56

    please create a video on content projection.

  • @HackHeyner
    @HackHeyner Před rokem +6

    Does not exist in the world, enough recognition for this guy! Absolute unit of a teacher!

  • @sukdipkar8876
    @sukdipkar8876 Před 18 dny

    Nobody in youtube teaches angular like this depth as you! Thank you very much and keep teaching us such valuable concepts.

  • @djedaaa2232
    @djedaaa2232 Před rokem +17

    I just want to say, i’m crazy about your courses and tutorials. You really have a gift for teaching, you dive deep into subject and talk about complex things which others often avoid do to the complexity. This channel (and also your paid courses) are such a refresher for Angular community.. Keep up the great work!

  • @advaithsurya4039
    @advaithsurya4039 Před rokem +6

    I absolutely love angular and [ngTemplateOutlet] is one of my favourite topics because they allow my template to be extremely reusable. It is indeed very powerful in terms of functionality.

  • @samuelalejandro2104
    @samuelalejandro2104 Před rokem +1

    I don't speak English (I'm Brazilian), but your explanation is so clear and concise that even if I don't know much English I can understand 98% of what you say. By the way, I love your content.

  • @davidwang5445
    @davidwang5445 Před rokem +14

    Another excellent video. Well structured, clearly expained. Three parts with small changes enhanced the concepts. Recap helps the viewers remember what has learned. Thank you so much and happy holidays!

    • @DecodedFrontend
      @DecodedFrontend  Před rokem

      Thank you, I am glad that you liked the video. Have a great holidays as well :)

  • @MrBau25
    @MrBau25 Před rokem

    Another great video!
    Thanks a lot for everything you do for us! I really learned a lot from your videos.
    Yes, we really want the next video with the ng-content tips and tricks.

  • @sergiocebrian85
    @sergiocebrian85 Před rokem +3

    Great job Dmytro! NgTemplateOutlet is a powerful directive that everyone should use often

  • @anujparashar444
    @anujparashar444 Před rokem

    what an amazing and useful content you are making. You don't know how much helpful it is in my daily routine work! Thanks a ton

  • @kendalljones99
    @kendalljones99 Před 7 měsíci

    Fantastic video, extremely helpful! Thank you!

  • @demidovmaxim1008
    @demidovmaxim1008 Před rokem

    Thank you! The best content about Angular.

  • @sercan272727
    @sercan272727 Před 2 měsíci

    this was exactly what i needed. thanks for the video

  • @rembautimes8808
    @rembautimes8808 Před 7 měsíci

    Been following this channel for several years. The tutorials are first class and really appreciate the effort that goes into making the video. I struggled with ng template outlet earlier but this helps a lot

  • @animetographyblogarchive9852

    I've learned so much from your videos. Your content covers topics I haven't found anywhere and it has helped me a lot in my work. Thank you so much!

  • @user-qn1if5gu8q
    @user-qn1if5gu8q Před 7 měsíci

    Дмитрий, спасибо большое! Очень понятно и подробно объясняете, одно удовольствие смотреть Ваши ролики.

  • @bryanbohol
    @bryanbohol Před rokem

    very useful vid! thank you for being generous to us. you are really a smart guy!

  • @rahultej8352
    @rahultej8352 Před rokem

    The way you explain things is not just bring out the solution but also clearly undertand the concepts.

  • @anhquande
    @anhquande Před rokem

    Another great video. Thanks ❤ and Happy new year 🎉🎉

  • @akhilanilkumar9090
    @akhilanilkumar9090 Před rokem

    Crisp and Clear. Good Work 👍

  • @jakubrakowski3429
    @jakubrakowski3429 Před rokem

    U make my learning curve easy now :). This is the missing spot for more advanced concepts. Cheers

  • @KvN-UA
    @KvN-UA Před rokem +1

    Thanks a lot for the explanation. It's amazing!
    I would like to know more about content projection.

  • @empathetic24671
    @empathetic24671 Před 9 měsíci

    Amazingly taught. Very informative. Thanks for this❤

  • @valderrama999
    @valderrama999 Před rokem

    Thaks for sharing. This content is gold

  • @mostafavii
    @mostafavii Před 3 měsíci

    Thanks for your greate video about template outelet. I've recently used it to create a dynamic mat-search-select. thanksssssss

  • @prangnana
    @prangnana Před rokem

    Simply Superb!!! I have been using ngTemplateOutlet from more than 2 years but still I leant new things from this video.
    Thanks Dmytro

  • @lonwabomsingelwa8833
    @lonwabomsingelwa8833 Před 4 měsíci

    Never really used this concept into depth but this video clearly put it out. 🥳

  • @mario_2211
    @mario_2211 Před rokem +2

    Great work
    I'd love to see a video on content projection

  • @hamiltonventura492
    @hamiltonventura492 Před měsícem

    Muito bom Parabéns, a qualidade está muito boa de seus vídeos continue assim amigo!

  • @AnwarulIslamYT
    @AnwarulIslamYT Před rokem

    I used to use NgTemplateOutlet and NgTemplateOutletContext directive to render repetitive code in the same component. It's really useful.

  • @jairzhino86
    @jairzhino86 Před rokem

    So good video..... Thank you so so much

  • @georgeknap2631
    @georgeknap2631 Před rokem

    There's never enough videos about ngTemplateOutlet 🤯

  • @williammendoza680
    @williammendoza680 Před 5 měsíci

    muchas gracias, excelente video, nuevo subs

  • @fnarsiste
    @fnarsiste Před rokem

    I'l new in the game and your videos are very useful -. Thanks

  • @heavierthanlight7173
    @heavierthanlight7173 Před 2 měsíci

    Late to the party but THNX A BUNCH! Really cewl feature!

  • @oleksiipopov5143
    @oleksiipopov5143 Před rokem +1

    Thank you! Very useful video!!!

  • @MikelAingeru
    @MikelAingeru Před rokem +1

    My most sincere congratulations.
    You have achieved something very difficult, explaining a complex Angular concept with a simple example and even showing useful tricks like $implicit, context and injector.
    Thank you very much!
    On the other hand, for me it is very strange to place the tags after the content in which they are going to be rendered. But I guess it's the only option to do it both inside and outside a component and without projection of .

  • @gagiksimonyan3782
    @gagiksimonyan3782 Před rokem +1

    Happy New Year, Dmytro) Wish you all the best:)

  • @santicanabalramos667
    @santicanabalramos667 Před rokem +7

    Really interesting video! I would like to have a video related to content projection as well :)

  • @harunmn
    @harunmn Před rokem +1

    another awesome lesson!

  • @SamiullahKhan
    @SamiullahKhan Před rokem

    Again, High Quality Content,
    I came across this templateOutlet many times but couldn't get it left alone it's benefits, Even the official docs didn't do the justice (maybe it's me that it didn't reflect with me).
    This time I think, you can't cherish the benefits or fruits of declarative approach if haven't gone through the imperative approach like you did with the examples. I know the effort and time it takes to come up with such example's kudos to that.
    Although I have more experience with Angular than Vue, but in Vue (I think) they are called scoped slot and I have used it well but coming to Angular I didn't get the templateContext it until this Good, Lengthy video. After knowing this concept now, I think Projected Content don't have luxury of Context. What else?

  • @claudiuciprianbetiuc3985

    Great video! I would also be interested in a video about content projection and when to use content projection / ngTemplateOutlet

  • @uidhtml
    @uidhtml Před 21 dnem

    U r awesome Dmytro.. ❤ from India Please keep making videos on important stuff of angular. Very helpful..

  • @ievgensvichkar2643
    @ievgensvichkar2643 Před rokem +3

    One more useful video, thanks a lot. Small remark: content projection can also be used in combination with ng-template approach when we use content projection to provide the template to be instantiated inside the recipient component (instead of using @Input for this).

    • @femelene2455
      @femelene2455 Před rokem +3

      That is exactly what I was thinking about at the end of the video. I think that Dmytro wanted to show us new Angular feature instead - ngTemplateOutlerInjector which is also very cool by the way :)

    • @DecodedFrontend
      @DecodedFrontend  Před 2 měsíci

      Hey guys :) Yes, you are right (you both :) ). Actually, many things could be accomplished in many different ways, and "the best" solution is usually very dependent on the particular details and your goals. The solution in the video was chosen just for the demonstration of the ngTemplateOutlerInjector feature staying in the same and familiar context of the WeatherComponent component.

  • @gagiksimonyan3782
    @gagiksimonyan3782 Před rokem +1

    Thanks for another useful video, Dmytro

  • @samiralholo2788
    @samiralholo2788 Před 8 měsíci

    You are the best thanks very much 3>

  • @TravisSomaroo
    @TravisSomaroo Před 7 měsíci

    Great stuff, would it be possible to have another video in the future for more advance use cases on ngTemplateOutlet as well as typing the templateOutlet with the Angular 16 features.

  • @weradsaoud2018
    @weradsaoud2018 Před 9 dny

    thank you

  • @MikelAingeru
    @MikelAingeru Před rokem

    ¡Gracias!

    • @DecodedFrontend
      @DecodedFrontend  Před rokem +1

      Thank you so much for your support! I appreciate it :)

  • @evgeniyshereverov5184
    @evgeniyshereverov5184 Před rokem +1

    Thank You!

  • @malyna
    @malyna Před rokem

    Thanks

  • @innocentmazando3808
    @innocentmazando3808 Před rokem +3

    Insightful lesson, I have a use case for it.
    Got a reusable table I need to wire next year and this may be the solution I am gonna run with

    • @DecodedFrontend
      @DecodedFrontend  Před rokem +2

      Yep, reusable table is a perfect use case and I think ngTemplateOutlet will perfectly work for you.

  • @VitaliiZubko
    @VitaliiZubko Před rokem +1

    Great explanation... Thanks. I will share this video with ours juniors, for better understanding...

  • @OfficialVideos482
    @OfficialVideos482 Před rokem +2

    Very Informative Pure Class 🙂

    • @DecodedFrontend
      @DecodedFrontend  Před rokem

      Thank you for your feedback! I am glad that you liked it.

  • @dumidupramith3881
    @dumidupramith3881 Před rokem

    thanks lot

  • @dev-gj3dh
    @dev-gj3dh Před 14 dny

    wow!!!

  • @Szergej33
    @Szergej33 Před rokem

    Please do content projection as well mate!
    After we trip[led our dev team size, we are juti n the process of standardising our components (so not every squad reinvents the dialog, the context-hint bar, etc).
    We have a bunch of good and keen Angular devs, but also many "fullstack :D" ppl not very comfortable with the framework.
    explaining to them how to create reusable components is so difficult, but this one is a great overview of embedded templates! Would be awesome to ahve content projection as well.

  • @pollo_cesar_
    @pollo_cesar_ Před rokem +1

    Great content and example, as always. One disadvantage of the ng-template context is that is not typified in when used (let-state) this could be a big problem on refactoring. I hope angular does something about this.

    • @DecodedFrontend
      @DecodedFrontend  Před rokem

      Yeah...I also hope that it will be supported out of the box.

  • @xingfucoder2627
    @xingfucoder2627 Před rokem +4

    Great video Dmytro. Would be great to have a course like the forms one, but for those Angular core middle or advanced topics.

    • @DecodedFrontend
      @DecodedFrontend  Před rokem +1

      This is very interesting. I plan 2 new courses in the next year. Let's see what i come up with ;)

    • @xingfucoder2627
      @xingfucoder2627 Před rokem

      @@DecodedFrontend Great Dmytro. Many thanks for your quick reply.

  • @MrVinodkumar92
    @MrVinodkumar92 Před rokem +2

    Can you pls explain content projection
    1. With slots
    2. With styling - do we need to add the styles in the projected component or in the component where the content is passed ?
    When viewencapsularion is emulated the ngcontent and nghost classes in Dom seems different
    Pls explain them as well.
    Thanks

    • @DecodedFrontend
      @DecodedFrontend  Před rokem +1

      Thank you for suggestion :) I will definitely cover those things

  • @adriannlara
    @adriannlara Před rokem +1

    Greate video, please make the ng-content video!!!

  • @FranklyTerrible
    @FranklyTerrible Před 11 měsíci

    Not going to lie, you're a damn legend. But I'm only left a bit with my head scratching around 29:43 - does injecting the Injector provide the reference of the WeatherWidgetComponent? That feels almost a bit "magical" to me! How was the problem with the lack of the component reference solved before? Did people simply pass the function into the ngTemplateOutletContext? I can't remember if you did any blog content for these injectors, but if you didn't I would really, really love those. Thank you for making things easier for us! Without you, I would have been lost in Angular long ago. Nobody touches upon more advanced topics like you do.

  • @davidontiveros2895
    @davidontiveros2895 Před rokem +1

    Content projection 🙏🏻

  • @haroldpepete
    @haroldpepete Před rokem +1

    Great video, thanks, i think that's an amazing feature

    • @DecodedFrontend
      @DecodedFrontend  Před rokem

      Yep, I am agree!

    • @haroldpepete
      @haroldpepete Před rokem +1

      @@DecodedFrontend i wish your country has a better year in that 2023, wich is very close and thanks for share your knowledge with us, good bless you, your family and your country

    • @DecodedFrontend
      @DecodedFrontend  Před rokem

      @@haroldpepete Thank you so much, I appreciate it so much.

  • @ugochukwuumerie6378
    @ugochukwuumerie6378 Před 3 měsíci

    How do you create a typed template context and template ref

  • @MoMoadeli
    @MoMoadeli Před rokem

    Awesome as usual. Quick correction: @ 9: 50 you mean IMPERATIVE paradigm. You then correctly reference implementing via a DECLARATIVE paradigm.

  • @SonicWalk
    @SonicWalk Před rokem

    very useful and helpful with of course high quality, not sure if my suggestion is right, because I find this speaking pace seems to me slow, maybe increase a bit speed of speaking would get more attractions. :)

  • @jonatabiondiJsLover
    @jonatabiondiJsLover Před rokem

    Great!! Just question. How di you access ngtemplate direttive file from vs code? If i ctrl +click the ngtemplateoutelet i see only the d.ts file and not the typescript.

  • @AntonioSantana-ll8il
    @AntonioSantana-ll8il Před rokem

    Hi!! greetings from Colombia!! you videos are excellent and help me a lot to improve my code a 100% Would be nice if you start teaching IONIC with angular!!! Thanks for exists bro!

  • @dav1tt
    @dav1tt Před rokem

    Great tming, hello I have lots of nesting in html template(I'm working with forms) and I was thinking aoubt using template to make it more readable. Is it good idea or should I solve in another way?

  • @claudiuciprianbetiuc3985

    Dmytro, seeing that not only in the comments of this video, but in others also, people that I guess (hope) to be subscribers of your channel mention topics they are interested in for the future videos of yours, I was wondering: would it make sense to you or could it be a win / win strategy if you would start something like a free options poll / list / for topics we would be interested in for your following CZcams videos / paid courses ? Just some food for thought. I know I would be interested in a few specific topics and possibly others also.

  • @MP3D2Y
    @MP3D2Y Před rokem

    Using the structural directive syntax *ngTemplateOutlet="templateName; context: { $implicit: variable, color: 'blue' }" we can specify all the information together. I suppose in Angular 14 they extend this to support the injector value

  • @geekybruce4819
    @geekybruce4819 Před 3 měsíci +1

    why not just give provide state & action at root or app.component level & inject both at other places where we want to use state & action since that will be one single instance available at all place? also is inject one component in other component good pratice ? cuz generally it is service that we inject

  • @akashchandran1
    @akashchandran1 Před 11 měsíci

    Could you create a video on angular universal?

  • @jorgecanas3668
    @jorgecanas3668 Před 4 měsíci

    I'm using your example and it works fine until I want to trigger an Output event on the parent component it doesn't work, but if I put a console.log inside the function I'm triggering the console log prints the data so it's communicating but for some reason it doesn't trigger the event, would you have any ideas as to why this is happening?

  • @Gyannea
    @Gyannea Před 9 měsíci

    Is it possible to create a single component whose template.html file is filled with a library of snippet which I can then use EVERYWHERE in all my other components?
    Much like a service gives us methods we can call from any component ts file. I am unable to get your examples to work, perhaps because I have template in files and NOT in the ts file. My html files have about a 1000 lines so it makes no sense to inline them.

  • @MahmoudTarek-pz1rl
    @MahmoudTarek-pz1rl Před 8 měsíci

    Im trying to build custom table using angular material table, providing custom table row using ngTemplateOutlet is not working or im missing something, any advice
    thanks

  • @glebzaitsev6741
    @glebzaitsev6741 Před rokem

    With Custom weather widget, it's neat but isn't it better to extract methods to reload and copy content to the injectable class? Then either provide it in root or, optionally, this injectable can be provided in parent class providers and injected into custom weather component via ngTemplateInjector.
    I mean, it's basically the same, just never seen using imported methods from components. Technically it's clearly possible, just feels wrong.
    Great video though, really helpful and clear, love it.

  • @Simon_IHG
    @Simon_IHG Před rokem

    Hi,
    Thanks for your video !
    Is this approach better than using @ContentChild ?
    The use of @ContentChild seems more "appropriate" as the custom template is defined inside my component selector rather than outside.
    Or does it depend on the use case ?
    If there are multiple components that need to override the template, the @Input will be more convenient as the template will be
    defined only once.
    Whereas it will be defined inside each component with the use of @ContentChild ?

  • @mrfun9841
    @mrfun9841 Před 11 měsíci

    💥

  • @danilo_teixeira
    @danilo_teixeira Před 8 měsíci

    How do you do to make the HTML code colored? When I use HTML in the component, it displays with only one color, all orange

  • @Bukratusi
    @Bukratusi Před rokem +1

    please make more and more videos please. we love u

  • @samuelalejandro2104
    @samuelalejandro2104 Před rokem

    Personally, I prefer using ngComponentPortal and using an interface to bridge the data. So I can associate each component with a key in an object and change the components through this key

  • @MP3D2Y
    @MP3D2Y Před rokem

    In the last example I feel like you allow a kind of circular dependency but the angular compiler does not report anything about it. Maybe I am wrong

  • @dibfibo
    @dibfibo Před rokem

    Im trying to pass method inside context. I want trigger this method with click. Now i'm able to pass function, but is triggered no only from click. Have u ever tried same thing?

  • @madeOfClay99
    @madeOfClay99 Před rokem +1

    Somehow I got distracted by your cat : )
    Excellent video Dmytro

  • @PNWAA
    @PNWAA Před rokem

    Informative video. I do have a question, why didn't we use ng-content for dynamic content in a widget container. Additionally, I think the widget component should be dummy (no data)

  • @utpaulsarkar9694
    @utpaulsarkar9694 Před rokem

    Please create video on ng-content

  • @Emekaelo
    @Emekaelo Před rokem +1

    How can the solution that the ngTemplateOutletInjector brings be done in Angular versions lower than 14?

    • @DecodedFrontend
      @DecodedFrontend  Před rokem

      You can provide injector (or particular service you need) of WeatherComponent via template context and then from the context provide to the target component as an input and fetch dependencies from there. Maybe there are another workarounds but I heard only about this one.

  • @MK-we4dl
    @MK-we4dl Před rokem

    Спасибо, Дмитрий, вы мой Сенсей! В очередной раз благодаря вашим наставлениям я решил две проблемы которые не давали мне спокойно спать)) Спасибо!!! Ждем проекцию контента.

  • @godes43
    @godes43 Před rokem

    One thing I don't understand (and the main reason I watched your video) is why didn't we pass the actions in the context for the template the same way we did for the state?
    I have a component which content can be changed by the user's template but I have a couple of actions I would like to execute in the component from the personalized templates.
    I tried passing the methods in the context, but when the execution reaches a reference to "this", "this" is undefined and I don't know why

  • @volodymyrholodivskyi502
    @volodymyrholodivskyi502 Před rokem +1

    You could set a type of TemplateRef using ngTemplateContextGuard

    • @DecodedFrontend
      @DecodedFrontend  Před rokem

      Yep, this is exactly the solution to fix typings issue but it also requires additional “dummy” directive for the type inversion. Just a ContextGuard in WeatherWidget won’t work unfortunately.

  • @souhaibkhadraoui9898
    @souhaibkhadraoui9898 Před rokem +1

    Can the template outlet use components that have their own template outlets, who in turn have their own template outlets (recursive template outlets-ish) ?

  • @theanswer1993
    @theanswer1993 Před rokem +1

    Watching this video made me realize I know nothing about templates after working 4 years with Angular. I mean I used all of this before but not like this 😅

    • @DecodedFrontend
      @DecodedFrontend  Před rokem +1

      You can't imagine how often it happens to me as well :D

  • @bonnes04
    @bonnes04 Před rokem

    🙏🙏🙏

  • @VinitNeogi
    @VinitNeogi Před rokem +1

    21:18 is there any way to access state within AppComponent class?

    • @DecodedFrontend
      @DecodedFrontend  Před rokem +1

      Yes. You can get a reference to WeatherWidgetComponent like `@ViewChild(WeatherWidgetComponent) widget!: WeatherWidgetComponent;` and then this.widget.state

    • @VinitNeogi
      @VinitNeogi Před rokem

      @@DecodedFrontend If state within WeatherWidgetComponent is protected, do we have any options. Something with ng-template or so?

  • @abdurrahmanahmed7057
    @abdurrahmanahmed7057 Před 5 měsíci

    Luka Doncic at it again!

  • @kailashpatisinghdeora3677

    Hi Dymtro, you content is really great and I am from India and I liked it so much, can I know how you check library source code in vs code itself in same project?

    • @DecodedFrontend
      @DecodedFrontend  Před rokem +1

      Hi :) It is actually not the "source code in the same project". I just had another VS code window with angular source code opened and on post-production I just cut the transition, so it looks like I jumped immediately to source code which is not really true.

    • @kailashpatisinghdeora3677
      @kailashpatisinghdeora3677 Před rokem

      @@DecodedFrontend oh okay thank you.

  • @syimykamatov8955
    @syimykamatov8955 Před 8 měsíci

    Good topic