{"id":25,"date":"2026-07-04T01:54:33","date_gmt":"2026-07-04T01:54:33","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/angular\/03-angular-components-and-templates\/"},"modified":"2026-07-04T09:42:47","modified_gmt":"2026-07-04T09:42:47","slug":"03-angular-components-and-templates","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/angular\/03-angular-components-and-templates\/","title":{"rendered":"Angular Components and Templates \u2014 Build Your First UI"},"content":{"rendered":"<p><!-- ktg-updated-banner --><\/p>\n<div class=\"ktg-updated-banner\" style=\"margin:0 0 1.25em;padding:0.75em 1em;background:#fefce8;border-left:4px solid #ca8a04;border-radius:4px;\">\n<p><strong>Updated July 4, 2026.<\/strong> Refreshed for Angular 21 and current SEO best practices.<\/p>\n<\/div>\n<p><!-- ktg-series-toc --><\/p>\n<nav class=\"ktg-series-toc\" aria-label=\"Angular tutorial table of contents\" style=\"margin:1.5em 0;padding:1em 1.25em;background:#f8fafc;border:1px solid #e2e8f0;border-radius:8px;\">\n<h2>Table of Contents<\/h2>\n<p>Follow the Angular tutorial series in order:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/01-angular-introduction\/\">Lesson 1: Angular Tutorial \u2014 Introduction (2026)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/02-angular-cli-setup\/\">Lesson 2: Angular CLI Setup \u2014 Create Your First App (Angular 21+)<\/a><\/li>\n<li><strong aria-current=\"page\">Lesson 3: Angular Components and Templates \u2014 Build Your First UI<\/strong><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/04-angular-routing\/\">Lesson 4: Angular Routing \u2014 Navigate Between Pages (Angular 21+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/05-angular-services-di\/\">Lesson 5: Angular Services and Dependency Injection (Angular 21+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/06-angular-http-client\/\">Lesson 6: Angular HTTP Client \u2014 Fetch Data from APIs (Angular 21+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/07-angular-forms\/\">Lesson 7: Angular Forms \u2014 Reactive Forms and Signal Forms Preview (Angular 21+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/\">\u2190 Back to Angular Tutorial hub<\/a><\/li>\n<\/ol>\n<\/nav>\n<p>This lesson covers <strong>Angular components and templates<\/strong> \u2014 the core skills for building any Angular interface in 21+. You will create standalone components, bind data to templates, use signals for local state, and apply modern control flow with <code>@if<\/code> and <code>@for<\/code>.<\/p>\n<p><strong>Prerequisites:<\/strong> Lessons 1\u20132 with a local Angular 21 app from <code>ng new<\/code>. <strong>Estimated time:<\/strong> 45\u201355 minutes.<\/p>\n<h4 id=\"standalone-components\">1. Standalone Components<\/h4>\n<p>In Angular 21, components are <strong>standalone<\/strong> by default \u2014 no NgModule wrapper required. A minimal component lives in <code>greeting-card.ts<\/code>:<\/p>\n<pre><code class=\"language-typescript\">import { Component, input } from '@angular\/core';\n\n@Component({\n  selector: 'app-greeting-card',\n  standalone: true,\n  template: `\n    &lt;h2&gt;Hello, {{ name() }}!&lt;\/h2&gt;\n    &lt;p&gt;Role: {{ role() }}&lt;\/p&gt;\n  `,\n})\nexport class GreetingCard {\n  name = input.required&lt;string&gt;();\n  role = input('Guest');\n}\n<\/code><\/pre>\n<p>Use the component in <code>app.html<\/code> after importing it in <code>app.ts<\/code>:<\/p>\n<pre><code class=\"language-typescript\">import { Component } from '@angular\/core';\nimport { GreetingCard } from '.\/greeting-card';\n\n@Component({\n  selector: 'app-root',\n  standalone: true,\n  imports: [GreetingCard],\n  templateUrl: '.\/app.html',\n})\nexport class App {}\n<\/code><\/pre>\n<pre><code class=\"language-html\">&lt;app-greeting-card name=\"Kindson\" role=\"Developer\" \/&gt;\n<\/code><\/pre>\n<figure style=\"margin:1.5em 0;text-align:center;\"><img decoding=\"async\" src=\"https:\/\/www.kindsonthegenius.com\/angular\/wp-content\/uploads\/2026\/07\/03-angular-components-and-templates-section-1.jpg\" alt=\"Angular UI components in a browser preview\" width=\"1200\" height=\"675\" loading=\"lazy\" style=\"max-width:100%;height:auto;border-radius:8px;border:1px solid #dee2e6;\" \/><figcaption style=\"font-size:0.85em;color:#666;margin-top:0.5em;\">Photo by <a href=\"https:\/\/unsplash.com\/@tirzavandijk?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Tirza van Dijk<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h4 id=\"interpolation\">2. Templates and Interpolation<\/h4>\n<p>Double curly braces <code>{{ '{{' }} expression {{ '}}' }}<\/code> display component data in the template. Expressions can be property access, function calls, or signal reads (note the <code>()<\/code> when reading a signal):<\/p>\n<pre><code class=\"language-html\">&lt;p&gt;Title: {{ '{{' }} title() {{ '}}' }}&lt;\/p&gt;\n<\/code><\/pre>\n<p>Keep templates readable \u2014 complex logic belongs in the component class or a computed signal.<\/p>\n<figure style=\"margin:1.5em 0;text-align:center;\"><img decoding=\"async\" src=\"https:\/\/www.kindsonthegenius.com\/angular\/wp-content\/uploads\/2026\/07\/03-angular-components-and-templates-section-2.jpg\" alt=\"Developer building Angular components on a laptop\" width=\"1200\" height=\"675\" loading=\"lazy\" style=\"max-width:100%;height:auto;border-radius:8px;border:1px solid #dee2e6;\" \/><figcaption style=\"font-size:0.85em;color:#666;margin-top:0.5em;\">Photo by <a href=\"https:\/\/unsplash.com\/@cgower?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Christopher Gower<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h4 id=\"bindings\">3. Property and Event Binding<\/h4>\n<p><strong>Property binding<\/strong> sets DOM or component properties:<\/p>\n<pre><code class=\"language-html\">&lt;button [disabled]=\"isLoading()\"&gt;Save&lt;\/button&gt;\n<\/code><\/pre>\n<p><strong>Event binding<\/strong> listens for DOM events:<\/p>\n<pre><code class=\"language-html\">&lt;button (click)=\"save()\"&gt;Save&lt;\/button&gt;\n<\/code><\/pre>\n<p><strong>Two-way binding<\/strong> (for forms) uses <code>[(ngModel)]<\/code> when you import <code>FormsModule<\/code> \u2014 covered in a future forms lesson.<\/p>\n<h4 id=\"signals\">4. Signals for Local State<\/h4>\n<p>Angular 21 encourages <strong>signals<\/strong> for UI state that changes over time:<\/p>\n<pre><code class=\"language-typescript\">import { Component, signal } from '@angular\/core';\n\n@Component({\n  selector: 'app-counter',\n  standalone: true,\n  template: `\n    &lt;p&gt;Count: {{ '{{' }} count() {{ '}}' }}&lt;\/p&gt;\n    &lt;button (click)=\"increment()\"&gt;+1&lt;\/button&gt;\n  `,\n})\nexport class Counter {\n  count = signal(0);\n\n  increment() {\n    this.count.update((n) =&gt; n + 1);\n  }\n}\n<\/code><\/pre>\n<p>Read signals in templates with <code>()<\/code>. Update with <code>set()<\/code>, <code>update()<\/code>, or <code>mutate()<\/code> on writable signals. For derived values, use <code>computed()<\/code> in later lessons.<\/p>\n<h4 id=\"control-flow\">5. Control Flow: @if and @for<\/h4>\n<p>Modern Angular replaces <code>*ngIf<\/code> and <code>*ngFor<\/code> with built-in control flow:<\/p>\n<pre><code class=\"language-html\">@if (isLoggedIn()) {\n  &lt;p&gt;Welcome back!&lt;\/p&gt;\n} @else {\n  &lt;p&gt;Please sign in.&lt;\/p&gt;\n}\n\n&lt;ul&gt;\n  @for (item of items(); track item.id) {\n    &lt;li&gt;{{ '{{' }} item.name {{ '}}' }}&lt;\/li&gt;\n  }\n&lt;\/ul&gt;\n<\/code><\/pre>\n<p>Always use <code>track<\/code> in <code>@for<\/code> for performance \u2014 typically the item&#8217;s unique id.<\/p>\n<h4 id=\"hands-on\">6. Hands-On: GreetingCard with Signals<\/h4>\n<p>Combine inputs and signals in one component:<\/p>\n<pre><code class=\"language-typescript\">import { Component, input, signal, computed } from '@angular\/core';\n\n@Component({\n  selector: 'app-greeting-card',\n  standalone: true,\n  template: `\n    &lt;article class=\"card\"&gt;\n      &lt;h3&gt;{{ '{{' }} greeting() {{ '}}' }}&lt;\/h3&gt;\n      @if (showRole()) {\n        &lt;p&gt;Role: {{ '{{' }} role() {{ '}}' }}&lt;\/p&gt;\n      }\n      &lt;button (click)=\"toggleRole()\"&gt;Toggle role&lt;\/button&gt;\n    &lt;\/article&gt;\n  `,\n  styles: [\n    `.card { padding: 1rem; border: 1px solid #e2e8f0; border-radius: 8px; }`,\n  ],\n})\nexport class GreetingCard {\n  name = input.required&lt;string&gt;();\n  role = input('Developer');\n  showRole = signal(true);\n\n  greeting = computed(() =&gt; `Hello, ${this.name()}!`);\n\n  toggleRole() {\n    this.showRole.update((v) =&gt; !v);\n  }\n}\n<\/code><\/pre>\n<p>Import <code>GreetingCard<\/code> in your root component and pass props from the parent template.<\/p>\n<h4 id=\"next-steps\">7. Next Steps and Advanced Tutorials<\/h4>\n<p>You can now build interactive UIs with standalone components, bindings, signals, and control flow. Coming soon in this series: routing, services, HTTP, and forms.<\/p>\n<p>For full-stack Angular (CRUD, Spring Boot, AI chat), continue on <a href=\"https:\/\/www.munonye.com\/angular-tutorials\/\">munonye.com Angular tutorials<\/a> after completing this beginner track.<\/p>\n<h4 id=\"faq\">Frequently Asked Questions<\/h4>\n<p><strong>What is a standalone component in Angular?<\/strong><br \/>\nA component that declares its own dependencies via the <code>imports<\/code> array in <code>@Component<\/code>, without an NgModule. Standalone is the default for new Angular 21 projects.<\/p>\n<p><strong>Should I use signals or RxJS for UI state?<\/strong><br \/>\nUse signals for local component and template state in Angular 21+. Use RxJS for HTTP streams, WebSockets, and complex async pipelines \u2014 often via <code>toSignal()<\/code> at the boundary.<\/p>\n<p><strong>Can I still use *ngIf and *ngFor?<\/strong><br \/>\nYes in legacy code, but new code should use <code>@if<\/code> and <code>@for<\/code> \u2014 they are faster, easier to read, and align with current Angular documentation.<\/p>\n<p><!-- ktg-faq-schema --><br \/>\n<script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What is a standalone component in Angular?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A component that declares its own imports in @Component without an NgModule wrapper.\"}},{\"@type\":\"Question\",\"name\":\"Should I use signals or RxJS for UI state?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use signals for local UI state in Angular 21+. Use RxJS for HTTP and async streams.\"}}]}<\/script><\/p>\n<p><!-- ktg-lesson-nav --><\/p>\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/www.kindsonthegenius.com\/angular\/02-angular-cli-setup\/\">Lesson 2: Angular CLI Setup \u2014 Create Your First App (Angular 21+)<\/a><\/p>\n<p><strong>Next:<\/strong> <a href=\"https:\/\/www.kindsonthegenius.com\/angular\/04-angular-routing\/\">Lesson 4: Angular Routing \u2014 Navigate Between Pages (Angular 21+)<\/a><\/p>\n<\/nav>\n<p><!-- ktg-alkademy-cta --><\/p>\n<div class=\"ktg-alkademy-cta\">\n<p><strong>Want live Angular or frontend classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led Angular and frontend courses with hands-on projects.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Updated July 4, 2026. Refreshed for Angular 21 and current SEO best practices. Table of Contents Follow the Angular tutorial series in order: Lesson 1: Angular Tutorial \u2014 Introduction (2026) Lesson 2: Angular CLI Setup \u2014 Create Your First App (Angular 21+) Lesson 3: Angular Components and Templates \u2014 Build Your First UI Lesson 4: &#8230; <a title=\"Angular Components and Templates \u2014 Build Your First UI\" class=\"read-more\" href=\"https:\/\/www.kindsonthegenius.com\/angular\/03-angular-components-and-templates\/\" aria-label=\"Read more about Angular Components and Templates \u2014 Build Your First UI\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":22,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":5,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":68,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/posts\/25\/revisions\/68"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/media\/22"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}