{"id":71678,"date":"2020-01-10T16:11:18","date_gmt":"2020-01-10T10:41:18","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=71678"},"modified":"2024-04-12T14:22:38","modified_gmt":"2024-04-12T08:52:38","slug":"master-detail-components","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/","title":{"rendered":"Master\/Detail Components"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-7-developer-tutorials\/\" target=\"_blank\" rel=\"noreferrer noopener\">Go back to Tutorial<\/a><\/p>\n\n\n<p>At the moment, the HeroesComponent displays both the list of heroes and the selected hero&#8217;s details. Keeping all features in one component as the application grows will not be maintainable. You&#8217;ll want to split up large components into smaller sub-components, each focused on a specific task or workflow.<\/p>\n<p>In this page, you&#8217;ll take the first step in that direction by moving the hero details into a separate, reusable HeroDetailComponent. The HeroesComponent will only present the list of heroes. The HeroDetailComponent will present details of a selected hero.<\/p>\n<h3><strong>Make the HeroDetailComponent<\/strong><\/h3>\n<p>Use the Angular CLI to generate a new component named hero-detail.<\/p>\n<p>ng generate component hero-detail<\/p>\n<p>The command scaffolds the following:<\/p>\n<ul>\n<li>Creates a directory src\/app\/hero-detail.<\/li>\n<\/ul>\n<p>Inside that directory four files are generated:<\/p>\n<ul>\n<li>A CSS file for the component styles.<\/li>\n<li>An HTML file for the component template.<\/li>\n<li>A TypeScript file with a component class named HeroDetailComponent.<\/li>\n<li>A test file for the HeroDetailComponent class.<\/li>\n<\/ul>\n<p>The command also adds the HeroDetailComponent as a declaration in the @NgModule decorator of the src\/app\/app.module.ts file.<\/p>\n<h3><strong>Write the template<\/strong><\/h3>\n<p>Cut the HTML for the hero detail from the bottom of the HeroesComponent template and paste it over the generated boilerplate in the HeroDetailComponent template.<\/p>\n<p>The pasted HTML refers to a selectedHero. The new HeroDetailComponent can present any hero, not just a selected hero. So replace &#8220;selectedHero&#8221; with &#8220;hero&#8221; everywhere in the template.<\/p>\n<p>When you&#8217;re done, the HeroDetailComponent template should look like this:<\/p>\n<p><strong>src\/app\/hero-detail\/hero-detail.component.html<\/strong><\/p>\n<p>&lt;div *ngIf=&#8221;hero&#8221;&gt;<\/p>\n<p>&lt;h2&gt;{{hero.name | uppercase}} Details&lt;\/h2&gt;<\/p>\n<p>&lt;div&gt;&lt;span&gt;id: &lt;\/span&gt;{{hero.id}}&lt;\/div&gt;<\/p>\n<p>&lt;div&gt;<\/p>\n<p>&lt;label&gt;name:<\/p>\n<p>&lt;input [(ngModel)]=&#8221;hero.name&#8221; placeholder=&#8221;name&#8221;\/&gt;<\/p>\n<p>&lt;\/label&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>Add the @Input() hero property<\/p>\n<p>The HeroDetailComponent template binds to the component&#8217;s hero property which is of type Hero. Open the HeroDetailComponent class file and import the Hero symbol.<\/p>\n<p><strong>src\/app\/hero-detail\/hero-detail.component.ts (import Hero)<\/strong><\/p>\n<p>import { Hero } from &#8216;..\/hero&#8217;;<\/p>\n<p>The hero property must be an Input property, annotated with the @Input() decorator, because the external HeroesComponent will bind to it like this.<\/p>\n<p>&lt;app-hero-detail [hero]=&#8221;selectedHero&#8221;&gt;&lt;\/app-hero-detail&gt;<\/p>\n<p>Amend the @angular\/core import statement to include the Input symbol.<\/p>\n<p><strong>src\/app\/hero-detail\/hero-detail.component.ts (import Input)<\/strong><\/p>\n<p>import { Component, OnInit, Input } from &#8216;@angular\/core&#8217;;<\/p>\n<p>Add a hero property, preceded by the @Input() decorator.<\/p>\n<p>@Input() hero: Hero;<\/p>\n<p>That&#8217;s the only change you should make to the HeroDetailComponent class. There are no more properties. There&#8217;s no presentation logic. This component simply receives a hero object through its hero property and displays it.<\/p>\n<p><strong>Show the HeroDetailComponent<\/strong><\/p>\n<p>The HeroesComponent is still a master\/detail view. It used to display the hero details on its own, before you cut that portion of the template. Now it will delegate to the HeroDetailComponent.<\/p>\n<p>The two components will have a parent\/child relationship. The parent HeroesComponent will control the child HeroDetailComponent by sending it a new hero to display whenever the user selects a hero from the list.<\/p>\n<p>You won&#8217;t change the HeroesComponent class but you will change its template.<\/p>\n<p><strong>Update the HeroesComponent template<\/strong><\/p>\n<p>The HeroDetailComponent selector is &#8216;app-hero-detail&#8217;. Add an &lt;app-hero-detail&gt; element near the bottom of the HeroesComponent template, where the hero detail view used to be.<\/p>\n<p>Bind the HeroesComponent.selectedHero to the element&#8217;s hero property like this.<\/p>\n<p><strong>heroes.component.html (HeroDetail binding)<\/strong><\/p>\n<p>&lt;app-hero-detail [hero]=&#8221;selectedHero&#8221;&gt;&lt;\/app-hero-detail&gt;<\/p>\n[hero]=&#8221;selectedHero&#8221; is an Angular property binding.<\/p>\n<p>It&#8217;s a one way data binding from the selectedHero property of the HeroesComponent to the hero property of the target element, which maps to the hero property of the HeroDetailComponent.<\/p>\n<p>Now when the user clicks a hero in the list, the selectedHero changes. When the selectedHero changes, the property binding updates hero and the HeroDetailComponent displays the new hero.<\/p>\n<p>The revised HeroesComponent template should look like this:<\/p>\n<p><strong>heroes.component.html<\/strong><\/p>\n<p>&lt;h2&gt;My Heroes&lt;\/h2&gt;<\/p>\n<p>&lt;ul class=&#8221;heroes&#8221;&gt;<\/p>\n<p>&lt;li *ngFor=&#8221;let hero of heroes&#8221;<\/p>\n[class.selected]=&#8221;hero === selectedHero&#8221;<\/p>\n<p>(click)=&#8221;onSelect(hero)&#8221;&gt;<\/p>\n<p>&lt;span class=&#8221;badge&#8221;&gt;{{hero.id}}&lt;\/span&gt; {{hero.name}}<\/p>\n<p>&lt;\/li&gt;<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p>&lt;app-hero-detail [hero]=&#8221;selectedHero&#8221;&gt;&lt;\/app-hero-detail&gt;<\/p>\n<p>The browser refreshes and the app starts working again as it did before.<\/p>\n<p><strong>What changed?<\/strong><\/p>\n<p>As before, whenever a user clicks on a hero name, the hero detail appears below the hero list. Now the HeroDetailComponent is presenting those details instead of the HeroesComponent.<\/p>\n<p>Refactoring the original HeroesComponent into two components yields benefits, both now and in the future:<\/p>\n<ul>\n<li>You simplified the HeroesComponent by reducing its responsibilities.<\/li>\n<li>You can evolve the HeroDetailComponent into a rich hero editor without touching the parent HeroesComponent.<\/li>\n<li>You can evolve the HeroesComponent without touching the hero detail view.<\/li>\n<li>You can re-use the HeroDetailComponent in the template of some future component.<\/li>\n<\/ul>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-7-developer-tutorials\/\" target=\"_blank\" rel=\"noreferrer noopener\">Go back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go back to Tutorial At the moment, the HeroesComponent displays both the list of heroes and the selected hero&#8217;s details. Keeping all features in one component as the application grows will not be maintainable. You&#8217;ll want to split up large components into smaller sub-components, each focused on a specific task or workflow. In this page,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8294],"tags":[8319],"class_list":["post-71678","page","type-page","status-publish","hentry","category-angular-7","tag-master-detail-components"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Master\/Detail Components - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Master\/Detail Components - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Go back to Tutorial At the moment, the HeroesComponent displays both the list of heroes and the selected hero&#8217;s details. Keeping all features in one component as the application grows will not be maintainable. You&#8217;ll want to split up large components into smaller sub-components, each focused on a specific task or workflow. In this page,...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:52:38+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/\",\"name\":\"Master\/Detail Components - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-10T10:41:18+00:00\",\"dateModified\":\"2024-04-12T08:52:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Master\/Detail Components\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Master\/Detail Components - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/","og_locale":"en_US","og_type":"article","og_title":"Master\/Detail Components - Tutorial","og_description":"Go back to Tutorial At the moment, the HeroesComponent displays both the list of heroes and the selected hero&#8217;s details. Keeping all features in one component as the application grows will not be maintainable. You&#8217;ll want to split up large components into smaller sub-components, each focused on a specific task or workflow. In this page,...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:52:38+00:00","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/","name":"Master\/Detail Components - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-10T10:41:18+00:00","dateModified":"2024-04-12T08:52:38+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/master-detail-components\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Master\/Detail Components"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=71678"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71678\/revisions"}],"predecessor-version":[{"id":87832,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71678\/revisions\/87832"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=71678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=71678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=71678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}