{"id":14480,"date":"2023-04-19T11:51:58","date_gmt":"2023-04-19T06:21:58","guid":{"rendered":"https:\/\/code4developers.com\/?p=14480"},"modified":"2023-05-12T19:25:42","modified_gmt":"2023-05-12T13:55:42","slug":"angular-custom-elements","status":"publish","type":"post","link":"https:\/\/code4developers.com\/angular-custom-elements\/","title":{"rendered":"Angular Custom Elements: Creating Reusable Components with Angular"},"content":{"rendered":"<p>Angular Custom Elements allow you to create reusable components that can be used in non-Angular applications. Custom Elements are a powerful feature that can help you share code across multiple projects and platforms. In this article, we&#8217;ll take a closer look at Angular Custom Elements and show you how to create them with code.<\/p>\n<h6 id=\"what-are-angular-custom-elements\">What are Angular Custom Elements?<\/h6>\n<p><img  loading=\"lazy\"  decoding=\"async\"  data-attachment-id=\"14529\"  data-permalink=\"https:\/\/code4developers.com\/angular-custom-elements\/custom-elements\/\"  data-orig-file=\"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?fit=1200%2C1200&amp;ssl=1\"  data-orig-size=\"1200,1200\"  data-comments-opened=\"1\"  data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\"  data-image-title=\"Custom elements\"  data-image-description=\"\"  data-image-caption=\"\"  data-medium-file=\"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?fit=800%2C800&amp;ssl=1\"  data-large-file=\"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?fit=1160%2C1160&amp;ssl=1\"  class=\"wp-image-14529 alignright pk-lazyload\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Angular Custom Elements\"  width=\"202\"  height=\"202\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"auto, (max-width: 202px) 100vw, 202px\"  data-pk-src=\"https:\/\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png\"  data-pk-srcset=\"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?w=1200&amp;ssl=1 1200w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=800%2C800&amp;ssl=1 800w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=1160%2C1160&amp;ssl=1 1160w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=80%2C80&amp;ssl=1 80w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=120%2C120&amp;ssl=1 120w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=90%2C90&amp;ssl=1 90w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=320%2C320&amp;ssl=1 320w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=560%2C560&amp;ssl=1 560w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=160%2C160&amp;ssl=1 160w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=240%2C240&amp;ssl=1 240w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=180%2C180&amp;ssl=1 180w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=640%2C640&amp;ssl=1 640w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=1120%2C1120&amp;ssl=1 1120w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=45%2C45&amp;ssl=1 45w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=300%2C300&amp;ssl=1 300w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=700%2C700&amp;ssl=1 700w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2023\/04\/Custom-elements.png?resize=768%2C768&amp;ssl=1 768w\" ><\/p>\n<p>Angular Custom Elements are a way of creating custom HTML elements using Angular components. Custom Elements are part of the Web Components specification, which is a set of APIs that allow you to create reusable components for the web. Custom Elements can be used in any web application or framework, and they work seamlessly with other web technologies like React and Vue.<\/p>\n<h6 id=\"creating-angular-custom-elements-with-code\">Creating Angular Custom Elements with Code<\/h6>\n<p>Creating an Angular Custom Element is a straightforward process that involves a few simple steps. First, you need to create an Angular component that you want to turn into a Custom Element. In this example, we&#8217;ll create a simple component that displays a message on the screen.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">import { Component, Input } from '@angular\/core';\r\n\r\n@Component({\r\n  selector: 'hello-world',\r\n  template: `\r\n    &lt;div&gt;\r\n      &lt;h1&gt;{{message}}&lt;\/h1&gt;\r\n    &lt;\/div&gt;\r\n  `,\r\n})\r\nexport class HelloWorldComponent {\r\n  @Input() message = 'Hello, World!';\r\n}<\/pre>\n<p>Next, you need to create a new module that exports the component you just created. This module will be used to create the Custom Element.<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">import { NgModule, Injector } from '@angular\/core';\r\nimport { createCustomElement } from '@angular\/elements';\r\nimport { HelloWorldComponent } from '.\/hello-world.component';\r\n\r\n@NgModule({\r\n  declarations: [HelloWorldComponent],\r\n  entryComponents: [HelloWorldComponent],\r\n  exports: [HelloWorldComponent],\r\n})\r\nexport class HelloWorldModule {\r\n  constructor(private injector: Injector) {\r\n    const HelloWorldElement = createCustomElement(HelloWorldComponent, { injector });\r\n    customElements.define('hello-world', HelloWorldElement);\r\n  }\r\n  ngDoBootstrap() {}\r\n}<\/pre>\n<p>Finally, you need to bootstrap the module using the Angular <code>ngDoBootstrap()<\/code> method.<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">import { BrowserModule } from '@angular\/platform-browser';\r\nimport { NgModule, Injector } from '@angular\/core';\r\nimport { HelloWorldModule } from '.\/hello-world\/hello-world.module';\r\n\r\n@NgModule({\r\n  imports: [BrowserModule, HelloWorldModule],\r\n})\r\nexport class AppModule {\r\n  constructor(private injector: Injector) {}\r\n  ngDoBootstrap() {}\r\n}<\/pre>\n<p>With these three steps, you&#8217;ve successfully created an Angular Custom Element. You can now use this element in any HTML page, just like any other HTML element.<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;script src=\".\/hello-world.bundle.js\"&gt;&lt;\/script&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;hello-world message=\"Hello, World!\"&gt;&lt;\/hello-world&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h6 id=\"conclusion\">Conclusion<\/h6>\n<p>Angular Custom Elements are a powerful way to create reusable components that can be used across multiple platforms and projects. With just a few lines of code, you can create an Angular component and turn it into a Custom Element that can be used in any web application. Custom Elements are part of the Web Components specification and work seamlessly with other web technologies like React and Vue. If you&#8217;re looking for a way to share code across multiple projects and platforms, Angular Custom Elements are definitely worth considering.<\/p>\n<p>Don&#8217;t miss reading other articles on <strong><a href=\"https:\/\/code4developers.com\/category\/angular\/\">Angular<\/a><\/strong> on <a href=\"https:\/\/code4developers.com\/\">Code4Developers<\/a><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Angular Custom Elements allow you to create reusable components that can be used in non-Angular applications. Custom Elements are a powerful feature that can help you share code across multiple&hellip;<\/p>\n","protected":false},"author":4,"featured_media":9300,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[28],"tags":[],"powerkit_post_featured":[],"class_list":{"0":"post-14480","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-angular"},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2020\/06\/angular-10-now-available.png?fit=250%2C250&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8NAi4-3Ly","jetpack-related-posts":[{"id":4091,"url":"https:\/\/code4developers.com\/create-custom-pipes-aka-filters-in-angular-2-x\/","url_meta":{"origin":14480,"position":0},"title":"Create Custom Pipes &#8211; Filters in Angular 2.X +","author":"Arif Khoja","date":"March 10, 2019","format":false,"excerpt":"Our applications may have a lot of data for presentation, But for creating a good user experience it is preferable to see in a more understandable format like March 12, 2010 is better to read then Mon Mar 12 2010 15:23:40 GMT-0700 (Pacific Daylight Time). For such small and repeated\u2026","rel":"","context":"In &quot;Angular&quot;","block_context":{"text":"Angular","link":"https:\/\/code4developers.com\/category\/angular\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/angular-2.png?fit=240%2C240&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3047,"url":"https:\/\/code4developers.com\/apply-mcustomscrollbar-using-angular-directive\/","url_meta":{"origin":14480,"position":1},"title":"Apply mCustomScrollbar using Angular Directive","author":"Pawan Ingale","date":"August 14, 2017","format":false,"excerpt":"ngCustomScrollbar Directives are very well-known in the world of angular developers. Every developer knows the power of a\u00a0Directive. AngularJS has already provided many inbuilt directives which we are using on daily basis. Why custom scroll bars are useful? Browser default scroll bar are not so good to see while adjusting\u2026","rel":"","context":"In &quot;AngularJs&quot;","block_context":{"text":"AngularJs","link":"https:\/\/code4developers.com\/category\/angularjs\/"},"img":{"alt_text":"AngularJs LOGO","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/angular.png?fit=500%2C500&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3818,"url":"https:\/\/code4developers.com\/slicepipe-jsonpipe-asyncpipe\/","url_meta":{"origin":14480,"position":2},"title":"Pipes in Angular Part \u2013 4 : SlicePipe, JSONPipe, AsyncPipe","author":"Yatendrasinh Joddha","date":"August 19, 2018","format":false,"excerpt":"Here is the fourth part of angular pipe series in this part we will discuss about SlicePipe, JSONPipe, and AsyncPipe. If you have not read previous articles here is the link for previous articles. Pipes in Angular Part \u2013 1 : lowercase, uppercase, and titlecase pipes Pipes in Angular Part\u2026","rel":"","context":"In &quot;Angular&quot;","block_context":{"text":"Angular","link":"https:\/\/code4developers.com\/category\/angular\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/angular-2.png?fit=240%2C240&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3934,"url":"https:\/\/code4developers.com\/angular-version-7-released\/","url_meta":{"origin":14480,"position":3},"title":"Angular Version 7 Released","author":"Yatendrasinh Joddha","date":"October 19, 2018","format":false,"excerpt":"Finally, the Angular version 7 (Angular v7) is out with some awesome features. Thanks to Angular team for all the efforts, in this major release angular team is spanning the entire platform, with the core framework, Angular Material, and the CLI with corresponding key versions. This release covers new landscapes\u2026","rel":"","context":"In &quot;Angular&quot;","block_context":{"text":"Angular","link":"https:\/\/code4developers.com\/category\/angular\/"},"img":{"alt_text":"Virtual Scrolling","src":"https:\/\/i0.wp.com\/cdn-images-1.medium.com\/max\/1200\/1%2ACQKUmJrBs-523I4GOiEUaA.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/cdn-images-1.medium.com\/max\/1200\/1%2ACQKUmJrBs-523I4GOiEUaA.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/cdn-images-1.medium.com\/max\/1200\/1%2ACQKUmJrBs-523I4GOiEUaA.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3746,"url":"https:\/\/code4developers.com\/angular-pipes\/","url_meta":{"origin":14480,"position":4},"title":"Pipes in Angular Part &#8211; 1 : lowercase, uppercase, and titlecase pipes","author":"Yatendrasinh Joddha","date":"June 30, 2018","format":false,"excerpt":"In this series of article for the angular rookies we will discuss about pipes in Angular, a way to write display-value transformations that you can declare in your HTML. In this article we will discuss about lowercase, uppercase, and titlecase pipes. At the end of the series we will create\u2026","rel":"","context":"In &quot;Angular&quot;","block_context":{"text":"Angular","link":"https:\/\/code4developers.com\/category\/angular\/"},"img":{"alt_text":"example output","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2018\/06\/example-output.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3340,"url":"https:\/\/code4developers.com\/top-angularjs-interview-question\/","url_meta":{"origin":14480,"position":5},"title":"Top AngularJS interview questions","author":"Arif Khoja","date":"March 23, 2018","format":false,"excerpt":"Read Angular js interview questions including topic advanced topics like Dependency injection, Two-way binding, scope in angular js and many more. 1.Explain Directive scopes? There are three types of directive scopes available in Angular. Parent Scope: is default scope Child Scope: If the properties and functions you set on the\u2026","rel":"","context":"In &quot;AngularJs&quot;","block_context":{"text":"AngularJs","link":"https:\/\/code4developers.com\/category\/angularjs\/"},"img":{"alt_text":"AngularJs LOGO","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/angular.png?fit=500%2C500&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/14480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/comments?post=14480"}],"version-history":[{"count":11,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/14480\/revisions"}],"predecessor-version":[{"id":14531,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/14480\/revisions\/14531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media\/9300"}],"wp:attachment":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media?parent=14480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/categories?post=14480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/tags?post=14480"},{"taxonomy":"powerkit_post_featured","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/powerkit_post_featured?post=14480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}