Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Use Interface in Angular with Example

How to Use Interface in Angular with Example?

Aman Jain, August 6, 2022March 16, 2024

Interface in angular or in any programming languages is pattern which bind component or class to use same implementation across the modules. In this article we will cover the use of interface in angular with example. In Angular interface can be used on service, factory, component or any type of class so we can assign a same architecture pattern to all classes.

As we know angular heavily use interface for a variable, array, objects and class so in this example we will use it in a component with array of object.

I will take you through with a simple example of employee name, id and status so that you can use interface in any version of angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 application.

What is Interface in Angular?

Interface in angular or any language its a architectural pattern which sets some rules for a class, method, property or object to be followed.

Let’s understand it quickly with an example

Suppose we have a object which have three properties name, id and status. name must be string, id must be a integer and status must be a enum. So while working in a team then multiple developers should follow this rule of object data types if we don’t apply the interface then any of the developer can assign any type of data type then it will produce run time errors so to bound these things we use interfaces.

How to make interface ?

In angular we can create interface anywhere and then we can import it in component, service or any classs as follow

export interface Employee {

    id: number;

    name: string;
    
    status: string

}

Here we created a interface using keyword interface then to use it globally we exported it and Employee is the name of interface that we can import and use in any class or component.

id is type of number , status and name is string so when we use it we can only define this type of data so let’s use it.

How to use interface in angular component ?

To use the interface in component we first need to import it as follow

import { Component } from '@angular/core';
import { Employee } from "./employee-struct.ts" 
  
@Component({
  selector: 'my-app',
  templateUrl: './employee.component.html',
  styleUrls: [ './employee.component.css' ]
})
export class EmployeeComponent  {
  name = 'Employee component';
    
  employes: Employee[] = [
    {id: 1, name: "james",status:"active"},
    {id: 2, name: "rock",status:"active"},
    {id: 3, name: "denis",status:"active"},
  ]
}

As you can see here we imported the employee interface then used it as employes: Employee[] = []

Also Read : Create custom form control in angular 12 / 13

Related

Angular Javascript angularinterface

Post navigation

Previous post
Next post

Related Posts

Javascript Laravel Ajax Autocomplete Using Select2

Laravel Customized Autocomplete Options Using Select2

July 18, 2022July 18, 2022

In this article we will learn to use Laravel Customized Autocomplete Options Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. Autocomplete search is mostly work of javascript and…

Read More
Javascript Directives in angular

What is directives and types of directives in angular?

September 1, 2021February 8, 2024

Directives in angular are custom tags and custom attributes of html which is controlled by class and decorators. Using directives we can change the behaviour, style and logic of a specific area. Example: helloworld.component.ts Xyz.compenent.html <hello-world></hello-world> Output:Hello world component/directive Types of Directives in Angular: 1. Component : Angular page is…

Read More
Javascript How to convert JSON string to JSON object in angular

How to convert JSON string to JSON object in angular ?

April 13, 2022November 6, 2023

Sometimes in our application we want to convert JSON string to JSON object because the format of response is JSON string rather then JSON object, so in this tutorial i will show you to use JSON string to object and vise versa. JSON are used to communicate between client and…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version