This example walks you through the fundamentals of an Angular application.
-
Angular relies on "components" to render the user interface in an application.
-
To see a component, open
src/app/app.component.ts
. -
Note the following:
- An import statement is at the top that imports Component.
- A Component decorator is defined with several properties including the
app-root
selector for the component and an inline HTML template - A component class is defined that includes a constructor and an ngOnInit() function.
-
Open
src/index.html
. -
Locate the element in the page. This acts as the root component for the entire application.
-
Install Node.js 8.10 or higher (https://nodejs.org) and an editor (VS Code is recommended - https://code.visualstudio.com) if you don't have them installed already.
-
Install the Angular CLI by opening a command prompt and running the following:
npm install -g @angular/cli
-
Create a new Angular project by running the following command:
ng new my-project
-
Once the project has been created
cd
into themy-project
directory and leave the console window open. -
Open
my-project
in an editor. -
Locate the
src/app/app.component.ts
file. Notice that the Component decorator is different than the previous one and references an external HTML template. Also notice that it includes a property calledtitle
. -
Open the HTML template for the component and notice the
{{ title }}
section. This is used to display the value of thetitle
property discovered earlier in the component. -
Build the project and view it in the browser by using the following command:
ng serve -o