A Single Page Application Using Angular v4

In this article, we are going to create a single page application in Angular 4. In this application, we can add different items from the text box and remove the items from the list, as shown below.

Output

Angular

This is what we are going to develop in this article using Angular 4.

Create a new project and open the below files in your Visual Studio Code.

  1. Component.ts
  2. Component.css
  3. Component.html

Open HTML file and paste the below code.

  1. <<div class="main">  
  2.     <h1>Latest Technologies </h1>  
  3.     <div class="addItem"> <input [(ngModel)]="newItem" placeholder="Add item" class="addText"> <button (click)="pushItem()">Add</button> </div>  
  4.     <ul>  
  5.         <li *ngFor="let i of items; let ind = index"> {{i}} <span (click)="removeItem(ind)">x</span> </li>  
  6.     </ul>  
  7.     </div>  

Output

Angular

Now, we need to write style components in CSS file. Open app.component.css and paste the below code.

Code

  1. .main {  
  2.     width: 500 px;  
  3.     text - align: center;  
  4.     margin: 0 auto;  
  5.     border: 2 px solid# d7d7d7;  
  6.     border - bottom: 0 px;  
  7.     margin - top: 20 px;  
  8.     font - family: sans - serif;  
  9. }  
  10. /*//Second Frame*/  
  11. h1 {  
  12.     text - align: center;  
  13.     background - color: purple;  
  14.     margin - top: 0 px;  
  15.     color: white;  
  16.     padding: 30 px 0 px;  
  17. }  
  18. /*//Third Frame*/  
  19. .addItem {  
  20.     position: relative;  
  21.     padding - bottom: 0 px;  
  22.     height: 30 px;  
  23. }.addText {  
  24.     width: 80 % ;  
  25.     height: 30 px;  
  26.     padding: 5 px;  
  27.     font - size: 20 px;  
  28. }.addItem button {  
  29.     height: 45 px;  
  30.     width: 50 px;  
  31.     padding: 5 px;  
  32. }  
  33. ul {  
  34.     list - style: none;  
  35.     font - style: 20 px;  
  36.     color: #686868;  
  37. margin-left: -40px;  
  38. margin-bottom: 0px;  
  39. }  
  40. li{  
  41. border-bottom: 2px solid # bfbfbf;  
  42.     background - color: #d7d7d7;  
  43.     padding: 10 px 0 px;  
  44.     margin - bottom: 5 px  
  45. }  
  46. span {  
  47.     cursor: pointer;  
  48.     position: relative;  
  49.     float: right;  
  50.     margin - right: 10 px  
  51. }  
Output

Angular

Once we decorate the CSS file, the output will be as above. Now, our task is to add a new item, and it should add and display in the below textbox.

First, open app.components.ts file and remove the text from the export class AppComponent.

Angular

Add the array list in the export class as below.

  1. Item=[“ASP.NET”,”MVC.NET”,”SharePoint”,”O365”];

Now, bind this item list to the ul element in HTML, as shown below.

  1. <ul>  
  2.     <li *ngFor="let i of items"> {{i}} <span>x</span> </li>  
  3. </ul>  

Output

Angular

Let’s write our code for adding a new item from the input text. Come back to app.component.ts file and add another variable that’s going to contain a new value to be written in the input text box.

So now, add a new item to the export class as an empty string.

  1. newItem= “”;

After that, we are going to define a function PushItem as shown below.

It’s going to have a function instance and first of all, we need to check if this.newitem is not equal to empty, then add a new item.

The code is given below.

  1. export class AppComponent {  
  2.     items = ["ASP.NET""MVC.NET""Sharepoint""O365"];  
  3.     newItem = "";  
  4.     pushItem = function() {  
  5.         if (this.newItem != "") items.pushItem(this.newItem);  
  6.     }  
This is how we push a new item into our array items. After that, we need to make the new item empty again as below.
  1. newItem=””;

Go back to the HTML page and bind newItem variable in the Input element, because if we write anything in the input element, it should reflect the change in the newItem variable. This is called two-way binding.

Code

  1. <div class="addItem">  
  2.    <input [(ngModel)]= "newItem" placeholder="Add item" class="addText">  
  3.    <button (click) = "pushItem()">Add</button>  
  4. </div>  
Add a button click and call pushItem function, as shown in the above snippet.

Let add removeItem function below pushItem function, as shown below.

  1. removeItem = function(index) {  
  2.     items.splice(index - 1);  
  3. }  
And the index will contain the index number of the array. So, from HTML, we can send the index number of the item that contains a list of elements.

Code

  1. <li *ngFor="let i of items; let ind = index" >  
  2. {{i}}  
  3. <span (click)= "remove Item(ind)">x</span>  
  4. </li>  
Finally, we can add and delete the items from the list. You can download the code from here.

Before

Angular
After we click "Add"

Angular