{"id":71674,"date":"2020-01-10T16:10:38","date_gmt":"2020-01-10T10:40:38","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=71674"},"modified":"2024-04-12T14:22:38","modified_gmt":"2024-04-12T08:52:38","slug":"setup-2","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/","title":{"rendered":"Setup"},"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 class=\"VSKILLbodytext\">Create a new project named angular-tour-of-heroes. The file structure should be as<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-113286\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2018\/05\/image054.jpg\" alt=\"\" width=\"213\" height=\"364\"><\/p>\n<h4>Keep the app transpiling and running<\/h4>\n<p>Enter the following command in the terminal window: npm start<\/p>\n<p>This command runs the TypeScript compiler in &#8220;watch mode&#8221;, recompiling automatically when the code changes. The command simultaneously launches the app in a browser and refreshes the browser when the code changes.<\/p>\n<p>You can keep building the Tour of Heroes without pausing to recompile or refresh the browser.<\/p>\n<h4>Show the hero<\/h4>\n<p>Add two properties to the AppComponent: a title property for the app name and a hero property for a hero named &#8220;Windstorm.&#8221;<\/p>\n<p><u>app.component.ts (AppComponent class)<\/u><\/p>\n<p>export class AppComponent {<\/p>\n<p>title = &#8216;Tour of Heroes&#8217;;<\/p>\n<p>hero = &#8216;Windstorm&#8217;;<\/p>\n<p>}<\/p>\n<p>Now update the template in the @Component decorator with data bindings to these new properties.<\/p>\n<p><u>app.component.ts (@Component)<\/u><\/p>\n<p>template: `&lt;h1&gt;{{title}}&lt;\/h1&gt;&lt;h2&gt;{{hero}} details!&lt;\/h2&gt;`<\/p>\n<p>The browser refreshes and displays the title and hero name. The double curly braces are Angular&#8217;s interpolation binding syntax. These interpolation bindings present the component&#8217;s title and hero property values, as strings, inside the HTML header tags.<\/p>\n<h4>Hero object<\/h4>\n<p>The hero needs more properties. Convert the hero from a literal string to a class. Create a Hero class with id and name properties. Add these properties near the top of the app.component.ts file, just below the import statement.<\/p>\n<p><u>src\/app\/app.component.ts (Hero class)<\/u><\/p>\n<p>export class Hero {<\/p>\n<p>id: number;<\/p>\n<p>name: string;<\/p>\n<p>}<\/p>\n<p>In the AppComponent class, refactor the component&#8217;s hero property to be of type Hero, then initialize it with an id of 1 and the name Windstorm.<\/p>\n<p><u>src\/app\/app.component.ts (hero property)<\/u><\/p>\n<p>hero: Hero = {<\/p>\n<p>id: 1,<\/p>\n<p>name: &#8216;Windstorm&#8217;<\/p>\n<p>};<\/p>\n<p>Because you changed the hero from a string to an object, update the binding in the template to refer to the hero&#8217;s name property.<\/p>\n<p><u>src\/app\/app.component.ts<\/u><\/p>\n<p>template: `&lt;h1&gt;{{title}}&lt;\/h1&gt;&lt;h2&gt;{{hero.name}} details!&lt;\/h2&gt;`<\/p>\n<p>The browser refreshes and continues to display the hero&#8217;s name.<\/p>\n<h4>Adding HTML with multi-line template strings<\/h4>\n<p>To show all of the hero&#8217;s properties, add a &lt;div&gt; for the hero&#8217;s id property and another &lt;div&gt; for the hero&#8217;s name. To keep the template readable, place each &lt;div&gt; on its own line.<\/p>\n<p>The backticks around the component template let you put the &lt;h1&gt;, &lt;h2&gt;, and &lt;div&gt; elements on their own lines, thanks to the template literals feature in ES2015 and TypeScript.<\/p>\n<p><u>app.component.ts (AppComponent&#8217;s template)<\/u><\/p>\n<p>template: `<\/p>\n<p>&lt;h1&gt;{{title}}&lt;\/h1&gt;<\/p>\n<p>&lt;h2&gt;{{hero.name}} details!&lt;\/h2&gt;<\/p>\n<p>&lt;div&gt;&lt;label&gt;id: &lt;\/label&gt;{{hero.id}}&lt;\/div&gt;<\/p>\n<p>&lt;div&gt;&lt;label&gt;name: &lt;\/label&gt;{{hero.name}}&lt;\/div&gt;<\/p>\n<p>`<\/p>\n<h4>Edit the hero name<\/h4>\n<p>Users should be able to edit the hero name in an &lt;input&gt; textbox. The textbox should both display the hero&#8217;s name property and update that property as the user types.<\/p>\n<p>You need a two-way binding between the &lt;input&gt; form element and the hero.name property. Refactor the hero name in the template so it looks like this:<\/p>\n<p><u>src\/app\/app.component.ts<\/u><\/p>\n<p>&lt;div&gt;<\/p>\n<p>&lt;label&gt;name: &lt;\/label&gt;<\/p>\n<p>&lt;input [(ngModel)]=&#8221;hero.name&#8221; placeholder=&#8221;name&#8221;&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n[(ngModel)] is the Angular syntax to bind the hero.name property to the textbox. Data flows in both directions: from the property to the textbox, and from the textbox back to the property.<\/p>\n<p>Unfortunately, immediately after this change, the application breaks. If you looked in the browser console, you&#8217;d see Angular complaining that &#8220;ngModel &#8230; isn&#8217;t a known property of input.&#8221;<\/p>\n<p>Although NgModel is a valid Angular directive, it isn&#8217;t available by default. It belongs to the optional FormsModule. You must opt-in to using that module.<\/p>\n<h4>Import the FormsModule<\/h4>\n<p>Open the app.module.ts file and import the FormsModule symbol from the @angular\/forms library. Then add the FormsModule to the @NgModule metadata&#8217;s imports array, which contains the list of external modules that the app uses. The updated AppModule looks like this:<\/p>\n<p><u>app.module.ts (FormsModule import)<\/u><\/p>\n<p>import { NgModule }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from &#8216;@angular\/core&#8217;;<\/p>\n<p>import { BrowserModule } from &#8216;@angular\/platform-browser&#8217;;<\/p>\n<p>import { FormsModule }&nbsp;&nbsp; from &#8216;@angular\/forms&#8217;; \/\/ &lt;&#8211; NgModel lives here<\/p>\n<p>import { AppComponent }&nbsp; from &#8216;.\/app.component&#8217;;<\/p>\n<p>@NgModule({<\/p>\n<p>imports: [<\/p>\n<p>BrowserModule,<\/p>\n<p>FormsModule \/\/ &lt;&#8211; import the FormsModule before binding with [(ngModel)]\n<p>],<\/p>\n<p>declarations: [<\/p>\n<p>AppComponent<\/p>\n<p>],<\/p>\n<p>bootstrap: [ AppComponent ]\n<p>})<\/p>\n<p>export class AppModule { }<\/p>\n<p>When the browser refreshes, the app should work again. You can edit the hero&#8217;s name and see the changes reflected immediately in the &lt;h2&gt; above the textbox.<\/p>\n<h4>Summary<\/h4>\n<p>Take stock of what you&#8217;ve built.<\/p>\n<ul>\n<li>The Tour of Heroes app uses the double curly braces of interpolation (a type of one-way data binding) to display the app title and properties of a Hero object.<\/li>\n<li>You wrote a multi-line template using ES2015&#8217;s template literals to make the template readable.<\/li>\n<li>You added a two-way data binding to the &lt;input&gt; element using the built-in ngModel directive. This binding both displays the hero&#8217;s name and allows users to change it.<\/li>\n<li>The ngModel directive propagates changes to every other binding of the hero.name.<\/li>\n<\/ul>\n<p>Here&#8217;s the complete app.component.ts as it stands now:<\/p>\n<p><u>src\/app\/app.component.ts<\/u><\/p>\n<p>import { Component } from &#8216;@angular\/core&#8217;;<\/p>\n<p>export class Hero {<\/p>\n<p>id: number;<\/p>\n<p>name: string;<\/p>\n<p>}<\/p>\n<p>@Component({<\/p>\n<p>selector: &#8216;my-app&#8217;,<\/p>\n<p>template: `<\/p>\n<p>&lt;h1&gt;{{title}}&lt;\/h1&gt;<\/p>\n<p>&lt;h2&gt;{{hero.name}} details!&lt;\/h2&gt;<\/p>\n<p>&lt;div&gt;&lt;label&gt;id: &lt;\/label&gt;{{hero.id}}&lt;\/div&gt;<\/p>\n<p>&lt;div&gt;<\/p>\n<p>&lt;label&gt;name: &lt;\/label&gt;<\/p>\n<p>&lt;input [(ngModel)]=&#8221;hero.name&#8221; placeholder=&#8221;name&#8221;&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>`<\/p>\n<p>})<\/p>\n<p>export class AppComponent {<\/p>\n<p>title = &#8216;Tour of Heroes&#8217;;<\/p>\n<p>hero: Hero = {<\/p>\n<p>id: 1,<\/p>\n<p>name: &#8216;Windstorm&#8217;<\/p>\n<p>};<\/p>\n<p>}<\/p>\n<h4>Next step<\/h4>\n<p>In the next tutorial page, you&#8217;ll build on the Tour of Heroes app to display a list of heroes. You&#8217;ll also allow the user to select heroes and display their details. You&#8217;ll learn more about how to retrieve lists and bind them to the template.<\/p>\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 Create a new project named angular-tour-of-heroes. The file structure should be as Keep the app transpiling and running Enter the following command in the terminal window: npm start This command runs the TypeScript compiler in &#8220;watch mode&#8221;, recompiling automatically when the code changes. The command simultaneously launches the app in a&#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":[8261],"class_list":["post-71674","page","type-page","status-publish","hentry","category-angular-7","tag-setup"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Setup - 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\/setup-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setup - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Go back to Tutorial Create a new project named angular-tour-of-heroes. The file structure should be as Keep the app transpiling and running Enter the following command in the terminal window: npm start This command runs the TypeScript compiler in &#8220;watch mode&#8221;, recompiling automatically when the code changes. The command simultaneously launches the app in a...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/\" \/>\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 property=\"og:image\" content=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2018\/05\/image054.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 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\/setup-2\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/\",\"name\":\"Setup - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2018\/05\/image054.jpg\",\"datePublished\":\"2020-01-10T10:40:38+00:00\",\"dateModified\":\"2024-04-12T08:52:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#primaryimage\",\"url\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2018\/05\/image054.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2018\/05\/image054.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Setup\"}]},{\"@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":"Setup - 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\/setup-2\/","og_locale":"en_US","og_type":"article","og_title":"Setup - Tutorial","og_description":"Go back to Tutorial Create a new project named angular-tour-of-heroes. The file structure should be as Keep the app transpiling and running Enter the following command in the terminal window: npm start This command runs the TypeScript compiler in &#8220;watch mode&#8221;, recompiling automatically when the code changes. The command simultaneously launches the app in a...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:52:38+00:00","og_image":[{"url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2018\/05\/image054.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/","name":"Setup - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2018\/05\/image054.jpg","datePublished":"2020-01-10T10:40:38+00:00","dateModified":"2024-04-12T08:52:38+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#primaryimage","url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2018\/05\/image054.jpg","contentUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2018\/05\/image054.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setup-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Setup"}]},{"@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\/71674","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=71674"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71674\/revisions"}],"predecessor-version":[{"id":87835,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71674\/revisions\/87835"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=71674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=71674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=71674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}