Skip to main content

Angular - Tutorial Part - 1 Application shell "Tour of languages"

The Application Shell

Install the Angular CLI

Install the Angular CLI, if you haven't already done so.
npm install -g @angular/cli

Create a new application

Create a new project named angular-tour-of-languages with this CLI command.
ng new angular-tour-of-languages


The Angular CLI generated a new project with a default application and supporting files.

Serve the application

Go to the project directory and launch the application.
cd angular-tour-of-heroes
ng serve --open
The ng serve command builds the app, starts the development server, watches the source files, and rebuilds the app as you make changes to those files.The --open flag opens a browser to http://localhost:4200/.

You should see the app running in your browser.

Angular components

The page you see is the application shell. The shell is controlled by an Angular component named AppComponent.
Components are the fundamental building blocks of Angular applications. They display data on the screen, listen for user input, and take action based on that input.

Change the application title

Open the project in your favorite editor or IDE and navigate to the src/app folder.
You'll find the implementation of the shell AppComponent distributed over three files:
  1. app.component.ts— the component class code, written in TypeScript.
  2. app.component.html— the component template, written in HTML.
  3. app.component.css— the component's private CSS styles.
Open the component class file (app.component.ts) and change the value of the title property to 'Tour of Heroes'.app.component.ts (class title property)

title = 'Tour of Heroes';
Open the component template file (app.component.html) and delete the default template generated by the Angular CLI. Replace it with the following line of HTML.app.component.html (template)

<h1>{{title}}</h1>
The double curly braces are Angular's interpolation binding syntax. This interpolation binding presents the component's title property value inside the HTML header tag.

The browser refreshes and displays the new application title.

Add application styles

Most apps strive for a consistent look across the application. The CLI generated an empty styles.css for this purpose. Put your application-wide styles there.
Here's an excerpt from the styles.css for the Tour of Languages sample app.


src/styles.css (excerpt)

  1. /* Application-wide Styles */
  2. h1 {
  3. color: #369;
  4. font-family: Arial, Helvetica, sans-serif;
  5. font-size: 250%;
  6. }
  7. h2, h3 {
  8. color: #444;
  9. font-family: Arial, Helvetica, sans-serif;
  10. font-weight: lighter;
  11. }
  12. body {
  13. margin: 2em;
  14. }
  15. body, input[text], button {
  16. color: #888;
  17. font-family: Cambria, Georgia;
  18. }
  19. /* everywhere else */
  20. * {
  21. font-family: Arial, Helvetica, sans-serif;
  22. }

Final code review

The source code for this tutorial and the complete Tour of Languages global styles are available in the live example / download example.
Here are the code files discussed on this page.
src/app/app.component.ts
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Tour of Heroes'; }

src/app/app.component.html
<h1>{{title}}</h1>

src/styles.css (excerpt)
  1. /* Application-wide Styles */
  2. h1 {
  3. color: #369;
  4. font-family: Arial, Helvetica, sans-serif;
  5. font-size: 250%;
  6. }
  7. h2, h3 {
  8. color: #444;
  9. font-family: Arial, Helvetica, sans-serif;
  10. font-weight: lighter;
  11. }
  12. body {
  13. margin: 2em;
  14. }
  15. body, input[text], button {
  16. color: #888;
  17. font-family: Cambria, Georgia;
  18. }
  19. /* everywhere else */
  20. * {
  21. font-family: Arial, Helvetica, sans-serif;
  22. }

Summary

  • You created the initial application structure using the Angular CLI.
  • You learned that Angular components display data.
  • You used the double curly braces of interpolation to display the app title.

Comments

Popular posts from this blog

.NET Core 2.0 Changes – 4 Key Things to Know

1. .NET Standard 2.0 Expanded APIs & the Ability to Reference Full Framework Libraries .NET Standard broadens the set of APIs available to include a lot of the missing features. It now supports 32,000+ APIs. It is now much easier to port your code to a .NET Standard library without major code changes. One of the biggest problems with .NET Core was the lack of third-party libraries. For example, when 1.0 came out, popular logging libraries like log4net were not even available (it is now). However, this was really only a problem if you wanted to deploy your app on Mac or Linux. You could have used .NET Core and targeted full .NET framework and not had these issues. .NET Standard 2.0 has added a  new compatibility shim  that will enable any .NET Core app to reference any full framework library. 2. Expanded OS Support One of the big goals with .NET Core is portability across multiple operating systems. Including desktops, servers, and even mobile. Microsoft ...

ASP.NET Core - MVC Design Pattern

The MVC (Model-View-Controller) design pattern is a design pattern that's actually been around for a few decades, and it's been used across many different technologies, everything from Smalltalk to C++ to Java and now in C# and .NET as a design pattern to use when you're building a user interface. The MVC design pattern is a popular design pattern for the user interface layer of a software application. In larger applications, you typically combine a model-view-controller UI layer with other design patterns in the application, like data access patterns and messaging patterns. These will all go together to build the full application stack. The MVC separates the user interface (UI) of an application into the following three parts − The Model  − A set of classes that describes the data you are working with as well as the business logic. The View  − Defines how the application’s UI will be displayed. It is a pure HTML which decides how the UI is going to loo...

ASP.NET Core - Create New Project

You can start building a new ASP.NET Core Application from the  File → New Project  menu option. On the New Project dialog box, you will see the following three different templates for Web projects − ASP.NET Web Application  − The simple ASP.NET application templates . ASP.NET Core Web Application (.NET Core)  − This will start you with a crossplatform compatible project that runs on the .NET Core framework. ASP.NET Core Web Application (.NET Framework)  − This starts a new project that runs on the standard .NET Framework on Windows. In the left pane, select  Templates → Visual C# → Web  and in the middle pane select the ASP.NET Core Web Application (.NET Core) template. Let us call this application  FirstAppDemo  and also specify the Location for your ASP.NET Core project and then Click OK. In the above dialog box, you can select a specific template for the ASP.NET application from the available ASP.NET Core Templates. ...