<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Viktor Slavov on Medium]]></title>
        <description><![CDATA[Stories by Viktor Slavov on Medium]]></description>
        <link>https://medium.com/@viktorslavov91?source=rss-d3bf47f75396------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QrO3zZfL22Nro0GC4CsFYQ.jpeg</url>
            <title>Stories by Viktor Slavov on Medium</title>
            <link>https://medium.com/@viktorslavov91?source=rss-d3bf47f75396------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 01:06:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@viktorslavov91/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular Schematics for Libraries]]></title>
            <link>https://medium.com/ignite-ui/angular-schematics-for-libraries-656159c90b57?source=rss-d3bf47f75396------2</link>
            <guid isPermaLink="false">https://medium.com/p/656159c90b57</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[libraries]]></category>
            <category><![CDATA[npm-package]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[schematics]]></category>
            <dc:creator><![CDATA[Viktor Slavov]]></dc:creator>
            <pubDate>Mon, 27 Apr 2020 00:00:00 GMT</pubDate>
            <atom:updated>2020-05-11T15:19:44.739Z</atom:updated>
            <content:encoded><![CDATA[<h4>Taking full advantage of Angular CLI’s ng-add and ng-update</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/0*AQZ-6NiwPYP6rLGs.jpg" /><figcaption>Photo by <a href="https://unsplash.com/@itfeelslikefilm"><strong>🇸🇮 Janko Ferlič</strong></a></figcaption></figure><p>Angular schematics have been around for some time now, and I think most of us have gotten used to working with them and enjoying their many benefits. From running a simple `ng new` to creating complex schematics that automate workflow and project setup, the schematics engine is a big part of any Angular application’s lifecycle.</p><p><strong><em>But what about an Angular library?</em></strong></p><p>At Infragistics, we take great pride in developing and maintaining our open source <a href="https://github.com/IgniteUI/igniteui-angular">Angular component library</a>. But with the library gaining more and more users, and the ever-evolving nature of Angular, we needed to provide an easy way to keep projects up-to- date. This is where, as with most things Angular, the schematics come into play.</p><h3>ng add, ng update and more</h3><p>In this blog, we’ll show you how to set up your library so that it takes full advantage of Angular CLI’s ng add and ng update commands. We will do this by defining schematic collections in a specific way.</p><p>We’ll be covering the following steps:</p><ol><li>ng add and ng update hooks</li><li>Setting up ng add schematics</li><li>Setting up ng update schematics (migrations)</li><li>Bundling and running your schematics</li></ol><p>In order to take full advantage of this post, ideally you should already be familiar with the basics of <a href="https://angular.io/guide/schematics">Angular schematics</a>, and you might be interested in some of the <a href="https://medium.com/@chrisbautistaaa/angular-schematics-going-a-bit-more-in-depth-f4858cd26fab">other</a> <a href="https://medium.com/rocket-fuel/angular-schematics-simple-schematic-76be2aa72850">great</a> <a href="https://medium.com/@tomastrajan/total-guide-to-custom-angular-schematics-5c50cf90cdb4">posts</a> on Medium.</p><p>To borrow from a recent angular.io article:</p><p><em>A schematic is a template-based code generator that supports complex logic. It is a set of instructions for transforming a software project by generating or modifying code. Schematics are packaged into </em><a href="https://angular.io/guide/glossary#collection"><em>collections</em></a><em> and installed with npm.</em></p><h3>Schematic entry points</h3><p>Aside from calling an Angular schematic with the usual</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1141535946ec7cdc59ff305cbee67f3a/href">https://medium.com/media/1141535946ec7cdc59ff305cbee67f3a/href</a></iframe><p>Angular’s CLI also provides two other entry points for your custom library workflows — ng add and ng update. If you are not familiar with these ng commands, you can learn more about them <a href="https://angular.io/cli/add">here</a> and <a href="https://angular.io/cli/update">here</a>. In an Angular application that’s using your library, these function similarly to npm’s postinstall hook. They are called automatically by the Angular CLI — when calling</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13f8827b912a1f22148ee4d29212c22f/href">https://medium.com/media/13f8827b912a1f22148ee4d29212c22f/href</a></iframe><p>Or</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4451ead2d33c2b66791d4c145a0d35ee/href">https://medium.com/media/4451ead2d33c2b66791d4c145a0d35ee/href</a></iframe><p>respectively.</p><h3>Migration Schematics</h3><p>One of the main benefits in using Angular’s CLI is updating your package dependencies while minimizing the need to manually workaround breaking changes or deprecations. All of this can be automated by defining migration schematics for your library!</p><p>A migration schematic is run each time a newer version of your library is added to the consuming application via ng update your-custom-library. In a migration schematic, you can not only define what changes your migration schematics should perform, but also specify the scope of the migration (which version it affects).</p><p><strong>Adding migration schematics</strong></p><p>Defining migration schematics is done in a similar way to defining a normal schematic — you have to create a function that returns a Rule. That rule can manipulate the work tree, log stuff and/or anything else that you can do with Javascript. The more specific part is making sure that the Angular CLI calls the schematic on the ng update hook.</p><p><strong>Defining the schematics collection</strong></p><p>First, you need to define a schematic collection.json file. The name doesn’t matter. In our library’s case, we’ve named it <a href="https://github.com/IgniteUI/igniteui-angular/blob/master/projects/igniteui-angular/migrations/migration-collection.json">migration-collection.json</a>, as we have multiple schematic collections and this makes it easier to tell which is which.</p><p>The structure of your collection.json should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/095a269977cdcc64622390811385a53a/href">https://medium.com/media/095a269977cdcc64622390811385a53a/href</a></iframe><p>The schematics object houses all of your update schematics definitions. Each definition is under a named property (which has to be unique) and for each one, you have to specify the version in which the “changes” are introduced and the factory for the schematic that will be run on the working tree. The version attribute is used to determine when you schematic needs to be run.</p><p>For example, let’s say you have an app consuming my-custom-library@1.0.0. With the above schematics definition, if you would run ng update my-custom-library@2.0.0, only the defined “version”: “6.0.0”schematic would be run. When running</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be6b46adbaa0d68988a11b2558c60728/href">https://medium.com/media/be6b46adbaa0d68988a11b2558c60728/href</a></iframe><p>both 2.0.0 and 3.0.0 migration schematics would be run.</p><p>The actual definition of the migration schematics (for v2.0.0, for example) is in ./update-2/index.ts can be like any other schematic. A very basic implementation would be this one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e3afea97b78f430a2b0090f2057585f/href">https://medium.com/media/6e3afea97b78f430a2b0090f2057585f/href</a></iframe><p>You can find the definition for one of the latest migration schematics in igniteui-angular <a href="https://github.com/IgniteUI/igniteui-angular/blob/master/projects/igniteui-angular/migrations/update-9_0_0/index.ts">here</a>.</p><p><strong><em>Disclaimer</em></strong><em>: In our library’s schematic implementation we are making of the TypeScript language service for manipulating files. I’m sorry for not going into details, but that would warrant a post of its own. If you have any questions, feel free to reach out in the comments and I’ll try to answer as best as I can.</em></p><h3>Attaching schematics collection to ng-update hook</h3><p>Once the migration collection.json is created and the migration schematics are defined, all that’s left is to properly attach them to the ng-update hook. All you need to do for that is to go over to your library’s package.json and add the following “ng-update” property:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a46df74ea0fdc8160207387213c7bce9/href">https://medium.com/media/a46df74ea0fdc8160207387213c7bce9/href</a></iframe><p>That’s it! Now every time someone runs ng update my-custom-library, the Angular CLI and the schematics that you’ve carefully crafted will take care of any pesky breaking changes or deprecations that a user might otherwise miss!</p><p><strong>ng-add</strong></p><p>The other helpful entry point that the Angular CLI provides is the ng add hook. When you define an ng add schematic, it is run automatically as soon as a user adds your library to their consuming project by running</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c09092868cce684e59d284588069ec1/href">https://medium.com/media/6c09092868cce684e59d284588069ec1/href</a></iframe><p>You can think of it as something akin to npm’s postinstall hook.</p><p>Defining an ng add schematic for your library is the best way to make sure that its users will have an easy first-time experience with it. Through the schematics, you can automate some mandatory initial setup, add/update packages or simply log a big “thank you!” to all your users! :)</p><p>In our library, we’ve added an ng add schematic that installs our <a href="https://github.com/IgniteUI/igniteui-cli">IgniteUI CLI</a> to the project workspace so users can take full advantage of our components suite via our CLI.<br> We make some initial transformations to the workspace (adding a custom .json file needed for our CLI to run), add some styles and packages (for our components to properly render) and even call some schematics (schematiception!) from our CLI, where we’ve offloaded some of the Angular CLI specific logic.<br> <br>All this (and more!) can be done automatically, simply by calling ng add — that, in my opinion, is the Angular CLI’s true charm.</p><h3>Create ng-add schematic</h3><p>To add an ng add schematic to your library, you first need to define it. As with any other schematic, it needs to be part of a collection. If your project already exposes schematics, you can add it to the existing collection under the name ng-add. If not, you can create the collection from scratch:</p><p>In your library’s root folder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d764df6363f3aac3183066037c6bb754/href">https://medium.com/media/d764df6363f3aac3183066037c6bb754/href</a></iframe><p>Open the newly created collection.json, modify it to point to the definition of the ng-add schematic</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce71975f7eab0347466e7ade3f291594/href">https://medium.com/media/ce71975f7eab0347466e7ade3f291594/href</a></iframe><p>Under schematics/ng-add/index.ts you can create the implementation of the ng-add schematic just as you would with any other schematic. For example, to display a simple “Thank you!” message to your users, you can do the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71f2f8968cf8b3e6f22ce032c20b0918/href">https://medium.com/media/71f2f8968cf8b3e6f22ce032c20b0918/href</a></iframe><p>The implementation of IgniteUI Angular’s ng-add schematic can be found <a href="https://github.com/IgniteUI/igniteui-angular/blob/master/projects/igniteui-angular/schematics/ng-add/index.ts">here</a>.</p><p><strong>Include in package.json</strong></p><p>Once you’re done with the implementation of the schematic, you have to include the collection definition in your library’s package.json. If your library already exposes a schematics collection, you’ve got this covered! If not, you just need to add a “schematics” property, pointing to the location of your newly created collection.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/119be5de4cc073ff0f1d1ef84620e5c9/href">https://medium.com/media/119be5de4cc073ff0f1d1ef84620e5c9/href</a></iframe><p>You can find this part of the setup reflected on our <a href="https://github.com/IgniteUI/igniteui-angular/blob/master/projects/igniteui-angular/package.json#L88">library repo</a>.</p><h3>Bundling your schematics</h3><p>You have to make sure that the schematics you’ve defined (both migrations and ng-add collections) are properly shipped with your library package. To do this, you will need to define a separate build task for your schematics (executed after the main build task has run), with outDir specified to point to the proper location in your project’s dist folder (so the collection.json files have the same paths, relative to the package.json). Finally, you will need to explicitly copy any files that are excluded by the compiler (for example, any .json files!).</p><p>Assuming that the output directory for your compiled projects is ../dist (so your project’s bundled files would be under ../dist/my-custom-lib/) your schematic’s compilation output will have to be ../dist/schematics (for the default schematic collection, containing ng-add) and ../dist/migrations (for the migration schematic collection)</p><p>To do this, you must first define a tsconfig.json file for your schematics collections (located in ./schematics/ or ./migrations, respectively). For example, the content of the file for the core schematics collection (./schematics) would be something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f78c17ece19a916a67230f90be9ecc4/href">https://medium.com/media/8f78c17ece19a916a67230f90be9ecc4/href</a></iframe><p>Then, in your project’s package.json, define a script that builds your schematics and another one that copies the files:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8c41a48df71d0dca86bf1193652f0b5/href">https://medium.com/media/b8c41a48df71d0dca86bf1193652f0b5/href</a></iframe><p>The same goes for the migrations — a separate script for TypeScript compile and for copying the files.</p><p>For the Ignite UI for Angular repo, you can check the links to see the implementation of the <a href="https://github.com/IgniteUI/igniteui-angular/blob/master/package.json#L27">default schematics</a> collection and the <a href="https://github.com/IgniteUI/igniteui-angular/blob/master/package.json#L26">migrations</a>. (Since we’re using gulp as a build tool, we’ve defined <a href="https://github.com/IgniteUI/igniteui-angular/blob/master/gulpfile.js#L128">separate tasks</a> there for copying the collection.json files)</p><p><strong>Running ng-add and ng-update schematics locally</strong></p><p>Before you publish your library package with your new and shiny ng update and ng add functionality, you might want to verify that the schematics are performing the proper workplace transformations. Since, in the end, both of these are just basic schematic implementations, you can run them as you would any other — by using ng g!</p><p>After building your library files and schematics + migrations, you can go into your specified output directory (where your bundle’s package.json is located) and pack it</p><pre>npm pack</pre><p><strong>Running ng-add</strong></p><p>To check how your ng-add is command is performing inside of a new project, you can do the following:</p><p>Create a new project using the Angular CLI:</p><pre>ng g my-test-project --routing=true</pre><p>Once the project is created, you can go into the project folder and install your package manually from the .tgz file:</p><pre>npm i my-custom-library.tgz</pre><p>Then, simply run</p><pre>ng g my-custom-library:ng-add</pre><p>and review the workplace change once it’s finished — that’s what your package’s ng-add will do!</p><p><strong>Running migrations</strong></p><p>For testing migrations, you can follow the same steps as the above, though you’ll have to specify the path to the migration schematics collection.json (as it’s different from the default schematics collection of the project).</p><p>Let’s say your latest migration schematic which you want to test is named update-1. Once you’ve manually installed your .tgz file, simply run:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fc1c2a6dea3304f1e4b73adcf7292b1/href">https://medium.com/media/6fc1c2a6dea3304f1e4b73adcf7292b1/href</a></iframe><p>This will apply all of the workplace changes that your latest migration schematic would apply.</p><p><strong>Closing thoughts</strong></p><p>Hopefully, this post has been helpful to you and has cleared up some things about Angular CLI’s ng add and ng update functionality. Angular’s schematics are now a solid part of their projects’ ecosystem and will surely continue improving. With <a href="https://www.youtube.com/watch?v=_LPzY9GrNwY">more</a> and <a href="https://www.youtube.com/watch?v=YyhLdDCend4">more</a> developers using them and raising awareness of them, now is a great time for libraries to start adopting schematics. Why not automate both you and your users worries away? :)</p><p>If you have any questions, feel free to reach out in the comments.</p><p>Thanks for reading!</p><p><em>Originally published at </em><a href="https://www.infragistics.com/community/blogs/b/engineering/posts/angular-schematics-for-libraries"><em>https://www.infragistics.com</em></a><em> on April 27, 2020.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=656159c90b57" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ignite-ui/angular-schematics-for-libraries-656159c90b57">Angular Schematics for Libraries</a> was originally published in <a href="https://medium.com/ignite-ui">Ignite UI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Angular’s ng-content in conjunction with ngProjectAs attribute]]></title>
            <link>https://medium.com/ignite-ui/using-ng-content-ngprojectas-1664d7c1d3b?source=rss-d3bf47f75396------2</link>
            <guid isPermaLink="false">https://medium.com/p/1664d7c1d3b</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Viktor Slavov]]></dc:creator>
            <pubDate>Fri, 08 Feb 2019 13:48:24 GMT</pubDate>
            <atom:updated>2020-10-26T03:55:54.018Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*w0IYwx-XQQ6MXUt1" /><figcaption>Photo by <a href="https://unsplash.com/@jeremyyappy?utm_source=medium&amp;utm_medium=referral">Jeremy Yap</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong>Introduction</strong></h3><p>I recently ran into some troubles when trying to project content across several component scopes. It took me quite a bit of searching until I found a way to make my solution work. Turns out content projection in Angular is a pretty complex and not-that-well documented topic. I came across and highly recommend reading <a href="https://medium.com/claritydesignsystem/ng-content-the-hidden-docs-96a29d70d11b">Eudes Petonnet’s <strong>ng-content: the hidden docs</strong></a><strong> </strong>— it provides a great and concise explanation on how Angular’s ng-content works in different scenarios.</p><p>All in all, documentation on the topic of content projection in Angular seems a bit lacking, so I thought I’d try my hand at providing a comprehensive example on how to use it and what pitfalls you might encounter.</p><h3>Setup</h3><p>So here’s the basic example I’ve been tweaking: I want to create a simple questionnaire component that can render both single and multiple choice questions. The component is data bound to a feed of questions. The questions, depending on their type, are then rendered either as a single or multi select component. You can find the working example <a href="https://stackblitz.com/edit/angular-survey-view-initial"><strong>here</strong></a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b61013001760f395b8132b6d034875db/href">https://medium.com/media/b61013001760f395b8132b6d034875db/href</a></iframe><p>I’ve also created a q-validator component, used to display an error message in a question (both q-single and q-multi). The validator is rendered inside of the question component’s host and will display an error if the question is not valid (in the example — if the value is null).</p><h3><strong>Using &lt;ng-content&gt;</strong></h3><p>Inside of the questionnaire template, I’ve defined 3 sections (list back-to-front for dramatic effect):</p><ul><li>Footer — Contains the survey submit button</li><li>Body — Contains all of the questions rendered in resp. components (q-single or q-multi)</li><li>Header — Contains any content that is passed inside of the q-questionnaire tags</li></ul><p>The header part of the survey is an example of the most basic use of ng-content — projecting everything that is inside of the element’s selector into the element’s host.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb4d2c69b0222670b00b0f15f3a31fa5/href">https://medium.com/media/bb4d2c69b0222670b00b0f15f3a31fa5/href</a></iframe><p>In the demo, I’m just passing some text inside of a h6 tag in the container, but for a more complex ‘greeting’, you can pass anything you like — standard tags, other components, template outlets.</p><h3><strong>Using &lt;ng-content select=”*”&gt;</strong></h3><p>If you need to further structure the content that is passed, you can use ng-content`’s select attribute. This selects all elements passed in the content that match the criteria of the select argument.<br>In the example, I’ll change the structure of the q-questionnaire header to render content as follows: everything passed with a qHeader attribute will be rendered inside of a h6 tag at the top of the .questionnaire__header section. Everything else will be rendered below it.</p><p>With these changes, the questionnaire template looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc8262b04fd4a5b9e8650c209636ebae/href">https://medium.com/media/bc8262b04fd4a5b9e8650c209636ebae/href</a></iframe><p>The &lt;ng-content select=&quot;[qHeader]&quot;&gt; always ‘gets’ content first. When all projections with `select` have passed, the rest is dumped in the &lt;ng-content&gt; catch all tag (if any). So now, if some header text is passed, it will always render on top of the questionnaire header in a h6 tag. Even if passed like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ffb3b91b1a82849d4fe8bd5bea6aa0b/href">https://medium.com/media/4ffb3b91b1a82849d4fe8bd5bea6aa0b/href</a></iframe><p><a href="https://stackblitz.com/edit/angular-survey-view-2"><strong>Updated Example</strong></a></p><h3><strong>Interlude — content projection woes</strong></h3><p><strong><em>Disclaimer</em></strong>: <em>This is a pretty messy way to pass content down through several components. For instance, having a reference to the questionnaire in the </em><em>QValidator component is way easier (and won’t interfere, as the validator is really a questionnaire only control).</em></p><p>My survey view is working properly — the question data is rendered correctly and the validator error messages show up when the questions are not valid. But since my error message is not tied to my questions data, I might want to make it customizable (for example, if the questions were all in German, currently my error messages will still be in English).</p><p>I add an input to my questionnaire component, accepting an object with error messages for each type of question (conveniently, only 2). I then pass the input down to the question components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1b8d66a05e53e3956292e2a489e7ebe/href">https://medium.com/media/e1b8d66a05e53e3956292e2a489e7ebe/href</a></iframe><p>and project it inside of the question components’ templates using ng-content select=&quot;[qError]&quot;:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e76f0bf7f744ab107326c27afcbdc9a3/href">https://medium.com/media/e76f0bf7f744ab107326c27afcbdc9a3/href</a></iframe><p>Inside of the validator, I want to handle the projected content with another content projection:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7196c98340552b061dfd588c4369bd3/href">https://medium.com/media/e7196c98340552b061dfd588c4369bd3/href</a></iframe><p>The content projection is handled with a select argument, in case we want to pass multiple content blocks down to the validator (which we will!).</p><p>In my survey view, I pass down custom error messages to my q-questionnaire via the errorMsg input:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/212da4ebbf4f8ba3bebfce4d5ce957ef/href">https://medium.com/media/212da4ebbf4f8ba3bebfce4d5ce957ef/href</a></iframe><p>When I run the app, no errors are thrown, but my validators do not show any error messages. The error icons are still visible, so the q-validator component is properly rendered for invalid questions.</p><p>If I debug my app, I can see that the error messages are properly passed to the q-questionnaire component (the instance of q-questionnaire.errorMsgs return the proper messages, in German). The question components are also getting a reference to the questionnaire (q-single.questionnaire is not <strong>null</strong>) and they are properly getting the errorMsgs (as they are <strong>public</strong>). If I change my q-multi.component.html to the following, I can even see the error message on my last question:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f53a3da0aee9f07b59709b0bc61a877/href">https://medium.com/media/2f53a3da0aee9f07b59709b0bc61a877/href</a></iframe><p>So whatever’s going wrong must be happening in the last step of the projection, when the message is being passed to the q-validator component.</p><h3><strong>Using &lt;ng-content ngProjectAs=”*”&gt;</strong></h3><p>The ng-content tag inside of the q-validator component does not seem to be properly rendering the error message.<br>If I change the template of the q-validator component to use a catch-all ng-content tag, the error message is displayed properly:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/884d2a1e0674703ad0a53698a1159dac/href">https://medium.com/media/884d2a1e0674703ad0a53698a1159dac/href</a></iframe><p>But what if I want to pass multiple content blocks and render them in pre-specified positions? I’d really like it if I could keep the select attribute on that ng-content tag.</p><p>I search around for any additional documentation regarding content projection in order to find something that can help me with my case. Eudes Petonnet’s <a href="https://medium.com/claritydesignsystem/ng-content-the-hidden-docs-96a29d70d11b">post</a> does a great job of explaining how ng-content works and has examples for different use cases.<br>In my example, the fault is in the projection, inside q-single/q-multi.component.html:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aad4d368daee507009fcf5212367198d/href">https://medium.com/media/aad4d368daee507009fcf5212367198d/href</a></iframe><p>Since the q-validator component is awaiting for a text passed with &lt;ng-content select=&quot;[qError]&quot;&gt;, it doesn’t recognize the block passed from q-single/q-multi (as the projection has not yet been replaced and the &lt;ng-content&gt; element itself <strong>does not</strong> match the criteria of `select=”[qError]”`). This can be solved by using the ngProjectAs attribute — this attribute can be placed on any element and allows it to ‘mask’ itself as the criteria passed, for the sake of content projection.</p><p>After adding the ngProjectAs=&quot;[qError]&quot; to the ng-content tag, the error messages appear correclty and are in German. Now the survey has customizable error messages that can be passed from the q-questionnaire component. It would be great if we could customize the error icon as well.</p><h3><strong>Using &lt;ng-template ngProjectAs=’*’&gt;</strong></h3><p>Okay, I want to pass an icon down to the validator component. I’ll create an @Input() property in the q-questionnaire that accepts a TemplateRef of the icon.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1ab60ffac2b27407026e075d674c669/href">https://medium.com/media/f1ab60ffac2b27407026e075d674c669/href</a></iframe><p>The icon is defined in the survey.component.html and the template ref is passed to the q-questionnaire via the input:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8af0ef5c742e2916bcfc807393705ae2/href">https://medium.com/media/8af0ef5c742e2916bcfc807393705ae2/href</a></iframe><p>I pass a reference to the questionnaire inside of the q-single/q-multi class and pass the questionnaire error icon down to the q-validator (using ngTemplateOutlet)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b0f24f7d5ab70aec8c68b3a2b3c559e/href">https://medium.com/media/3b0f24f7d5ab70aec8c68b3a2b3c559e/href</a></iframe><p>And, depending on whether the control has a custom icon passed or not, it renders the custom or default icon (resp.) in the validator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23a79f6dcc0481db4e740b390a23b2a8/href">https://medium.com/media/23a79f6dcc0481db4e740b390a23b2a8/href</a></iframe><p><strong><em>Disclaimer</em></strong><em>: Again, this is a far-from-ideal approach, but I think it is a good way to illustrate how content can be passed.</em></p><p>The QValidatorComponent.errorIcon gets a ContentChild by checking for an instance of QErrorIconDirective (using a ViewChild would always get a reference, due to the default icon). However, the ContentChild query never seems to return anything. I tried debugging and could see that the template was in fact passed to the questionnaire component (so QQuestionnaire.errorIcon was <strong>not</strong> <strong>undefined</strong>), thus was properly passed inside of the q-single/q-multi components and reached all the way down to the q-validator component (if the decorator on QValidatorComponent.errorIcon is changed to @ContentChild(TemplateRef, { read: TemplateRef }), the component properly gets a reference to the template and the custom icon is rendered).</p><h3><strong>Conclusion</strong></h3><p>I hope that this article and the examples will prove helpful to someone trying to better understand the use &lt;ng-content&gt;.</p><p>You can find the examples here:<br><a href="https://stackblitz.com/edit/angular-survey-view-initial"><strong>Initial State</strong></a><br><a href="https://stackblitz.com/edit/angular-survey-view-template-error"><strong>Error Messages</strong></a><br><a href="https://stackblitz.com/edit/angular-survey-view-error-icon"><strong>Error Icon</strong></a></p><p>And here is the GitHub repo, if you want to download and play with the demo:<br><a href="https://github.com/ViktorSlavov/DemoSurveyApp">https://github.com/ViktorSlavov/DemoSurveyApp</a></p><p>Feel free to share your thoughts on anything that comes to mind in the comments below.</p><p><strong>Thank you for reading!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1664d7c1d3b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ignite-ui/using-ng-content-ngprojectas-1664d7c1d3b">Using Angular’s ng-content in conjunction with ngProjectAs attribute</a> was originally published in <a href="https://medium.com/ignite-ui">Ignite UI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>