|
| 1 | +# Hello world |
| 2 | + |
| 3 | +This first lesson serves as the starting point from which each lesson in this tutorial adds new features to build a complete Angular app. In this lesson, we'll update the application to display the famous text, "Hello World". |
| 4 | + |
| 5 | +<docs-video src="https://www.youtube.com/embed/UnOwDuliqZA?si=uML-cDRbrxmYdD_9"/> |
| 6 | + |
| 7 | +## What you'll learn |
| 8 | + |
| 9 | +The updated app you have after this lesson confirms that you and your IDE are ready to begin creating an Angular app. |
| 10 | + |
| 11 | +NOTE: If you are working with the embedded editor, skip to [step four](#create-hello-world). |
| 12 | +When working in the browser playground, you do not need to `ng serve` to run the app. Other commands like `ng generate` can be done in the console window to your right. |
| 13 | + |
| 14 | +<docs-workflow> |
| 15 | + |
| 16 | +<docs-step title="Download the default app"> |
| 17 | +Start by clicking the "Download" icon in the top right pan of the code editor. This will download a `.zip` file containing the source code for this tutorial. Open this in your local Terminal and IDE then move on to testing the default app. |
| 18 | + |
| 19 | +At any step in the tutorial, you can click this icon to download the step's source code and start from there. |
| 20 | +</docs-step> |
| 21 | + |
| 22 | +<docs-step title="Test the default app"> |
| 23 | +In this step, after you download the default starting app, you build the default Angular app. |
| 24 | +This confirms that your development environment has what you need to continue the tutorial. |
| 25 | + |
| 26 | +In the **Terminal** pane of your IDE: |
| 27 | + |
| 28 | +1. In your project directory, navigate to the `first-app` directory. |
| 29 | +1. Run this command to install the dependencies needed to run the app. |
| 30 | + |
| 31 | + ```shell |
| 32 | + npm install |
| 33 | + ``` |
| 34 | + |
| 35 | +1. Run this command to build and serve the default app. |
| 36 | + |
| 37 | + ```shell |
| 38 | + ng serve |
| 39 | + ``` |
| 40 | + |
| 41 | + The app should build without errors. |
| 42 | + |
| 43 | +1. In a web browser on your development computer, open `http://localhost:4200`. |
| 44 | +1. Confirm that the default web site appears in the browser. |
| 45 | +1. You can leave `ng serve` running as you complete the next steps. |
| 46 | + </docs-step> |
| 47 | + |
| 48 | +<docs-step title="Review the files in the project"> |
| 49 | +In this step, you get to know the files that make up a default Angular app. |
| 50 | + |
| 51 | +In the **Explorer** pane of your IDE: |
| 52 | + |
| 53 | +1. In your project directory, navigate to the `first-app` directory. |
| 54 | +1. Open the `src` directory to see these files. |
| 55 | + 1. In the file explorer, find the Angular app files (`/src`). |
| 56 | + 1. `index.html` is the app's top level HTML template. |
| 57 | + 1. `styles.css` is the app's top level style sheet. |
| 58 | + 1. `main.ts` is where the app starts running. |
| 59 | + 1. `favicon.ico` is the app's icon, just as you would find in any web site. |
| 60 | + 1. In the file explorer, find the Angular app's component files (`/app`). |
| 61 | + 1. `app.ts` is the source file that describes the `app-root` component. |
| 62 | + This is the top-level Angular component in the app. A component is the basic building block of an Angular application. |
| 63 | + The component description includes the component's code, HTML template, and styles, which can be described in this file, or in separate files. |
| 64 | + |
| 65 | + In this app, the styles are in a separate file while the component's code and HTML template are in this file. |
| 66 | + |
| 67 | + 1. `app.css` is the style sheet for this component. |
| 68 | + 1. New components are added to this directory. |
| 69 | + |
| 70 | + 1. In the file explorer, find the image directory (`/assets`) that contains images used by the app. |
| 71 | + 1. In the file explorer, find the files and directories that an Angular app needs to build and run, but they are not files that you normally interact with. |
| 72 | + 1. `.angular` has files required to build the Angular app. |
| 73 | + 1. `.e2e` has files used to test the app. |
| 74 | + 1. `.node_modules` has the node.js packages that the app uses. |
| 75 | + 1. `angular.json` describes the Angular app to the app building tools. |
| 76 | + 1. `package.json` is used by `npm` (the node package manager) to run the finished app. |
| 77 | + 1. `tsconfig.*` are the files that describe the app's configuration to the TypeScript compiler. |
| 78 | + |
| 79 | +After you have reviewed the files that make up an Angular app project, continue to the next step. |
| 80 | +</docs-step> |
| 81 | + |
| 82 | +<docs-step title="Create `Hello World`"> |
| 83 | +In this step, you update the Angular project files to change the displayed content. |
| 84 | + |
| 85 | +In your IDE: |
| 86 | + |
| 87 | +1. Open `first-app/src/index.html`. |
| 88 | + NOTE: This step and the next are only for your local environment! |
| 89 | + |
| 90 | +1. In `index.html`, replace the `<title>` element with this code to update the title of the app. |
| 91 | + |
| 92 | + <docs-code header="Replace in src/index.html" path="adev/src/content/tutorials/first-app/steps/02-Home/src/index.html" visibleLines="[5]"/> |
| 93 | + |
| 94 | + Then, save the changes you just made to `index.html`. |
| 95 | + |
| 96 | +1. Next, open `first-app/src/app/app.ts`. |
| 97 | +1. In `app.ts`, in the `@Component` definition, replace the `template` line with this code to change the text in the app component. |
| 98 | + |
| 99 | + <docs-code language="angular-ts" header="Replace in src/app/app.ts" path="adev/src/content/tutorials/first-app/steps/02-Home/src/app/app.ts" visibleLines="[6,8]"/> |
| 100 | + |
| 101 | +1. In `app.ts`, in the `App` class definition, replace the `title` line with this code to change the component title. |
| 102 | + |
| 103 | + <docs-code header="Replace in src/app/app.ts" path="adev/src/content/tutorials/first-app/steps/02-Home/src/app/app.ts" visibleLines="[11,13]"/> |
| 104 | + |
| 105 | + Then, save the changes you made to `app.ts`. |
| 106 | + |
| 107 | +1. If you stopped the `ng serve` command from step 1, in the **Terminal** window of your IDE, run `ng serve` again. |
| 108 | +1. Open your browser and navigate to `localhost:4200` and confirm that the app builds without error and displays _Homes_ in the title and _Hello world_ in the body of your app: |
| 109 | + <img alt="browser frame of page displaying the text 'Hello World'" src="assets/images/tutorials/first-app/homes-app-lesson-01-browser.png"> |
| 110 | + </docs-step> |
| 111 | + |
| 112 | +</docs-workflow> |
| 113 | + |
| 114 | +SUMMARY: In this lesson, you updated a default Angular app to display _Hello world_. |
| 115 | +In the process, you learned about the `ng serve` command to serve your app locally for testing. |
| 116 | + |
| 117 | +For more information about the topics covered in this lesson, visit: |
| 118 | + |
| 119 | +<docs-pill-row> |
| 120 | + <docs-pill href="guide/components" title="Angular Components"/> |
| 121 | + <docs-pill href="tools/cli" title="Creating applications with the Angular CLI"/> |
| 122 | +</docs-pill-row> |
0 commit comments