Getting Started With Angular 6 - Part One

Introduction

There is some improvement in Angular 6 from the previous stable release version. Please check here. In this article, we will learn how to build our own Angular 6 application and,
  1. How to create a component
  2. How to call selectors
  3. How to add a stylesheet, header, footer, and dummy text to our application
To learn the basics, click here.
 
Step 1

Create a directory on your local computer. Open this folder using CMD. To install/update the latest Angular 6, use the below command.
  1. npm install -g @angular/cli  
Angular 6 command
 
To check the version of the user.
  1. ng -v  
 
Now, create a new project use the below command.
  1. ng new New-Project-Name
 
I am going to create the FirstApp project.
 
Step 2

Now, to run the application, open FirstApp folder and using the command prompt, type the below command which will compile your application and open it in the browser. Here, use -o for opening it. 
 
Note
If you are using Visual Studio Code, then right-click on the src folder and click on "Open a Terminal"; so you can run your project from here as well.
  1. ng server -o  
 Open a Terminal
If the port is being already used, then change the port by using the below command.
  1. ng serve --port New-Port-Number  
I am changing this port to 4201. Your output will look like this.

output 
Step 3

Create your own components as below. Create a new folder named 'components' in your project and open this in the terminal and use the below command to create a component.
  1. ng g c Your-Component-Name  
I am going to create Header component here. It will create HTML, CSS, spec.ts & ts files in the Header folder.

Note
Here, we have no need to use the "components" word while creating the components. For example, if you would like to create a Component Header, then use,
  1. ng g c Header  
 create component Header

In the Header HTML, I am creating some menus & a logo so it will look like the header component as below.
 
header.component.html 
  1. <div style="text-align:Left">    
  2.    <img width="200" alt="CodderFunda Logo" src="http://coderfunda.com/wp-content/uploads/2018/02/logo2.png">     
  3. </div>   
  4. <ul>  
  5.   <li><a href="#">C#</a></li>  
  6.   <li><a href="#">ASP.NET</a></li>  
  7.   <li><a href="#">Angular</a></li>  
  8. </ul>   
Step 4

Now, I am going to create a footer component as well and will write some copyrighted code in that. Create a component by using the above step 3 and write the below respective code in that.

In footer.component.css, I will write some CSS for footer content so it will look better.
 
footer.component.html
  1. <footer>  
  2.   <p>All rights reserved (c) 2018</p>  
  3. </footer>  
footer.component.css
  1. /* Style the footer */    
  2. footer {    
  3.     background-color#49a5f1;    
  4.     padding10px;    
  5.     text-aligncenter;    
  6.     colorwhite;    
  7. }     
Step 5

I am calling the selector in app.component.html file. Call the templates header & footer by using selector as,
  1. <app-header></app-header>  
Selector

The CSS selector identifies this directive in a template and triggers instantiation of the directive. In app.component.css, I will write some CSS for article content so it will look better.
 
app.component.html
  1. <app-header></app-header>  
  2. <article>  
  3. <p>  
  4.   What is Lorem Ipsum?  
  5. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.  
  6. </p>  
  7. </article>  
  8. <app-footer></app-footer>  
app.component.css
  1. article {  
  2.     padding20px;  
  3.     width98%;  
  4.     background-color#dae246;  
  5.     height120px;  
  6. }  
 Now, run the application and you will see the following.
 
application  
 
Summary

In this article, you learned how to create a project in Angular 6, how to create components in Angular 6, and some more basics.


Similar Articles