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

Softwares angular file upload

How to upload multiple files in angular 12 reactive form

June 20, 2021November 8, 2023

How to upload multiple files in angular 12 reactive form Uploading multiple files with progress in angular is too easy, you can implement it in your website using the below guideline. i will suggest to read my last post Angular file upload with post form data and php $_FILES to…

Read More

What is Access Modifiers ?

July 18, 2021August 2, 2021

In object oriented programming(oops) to set the visibility of classes, variables and methods we are using access modifiers. So Access modifiers are used the encapsulation of class properties. There are three types of access modifiers in any language (Php, java or c etc. ) Private Protected Public 1. Private Access…

Read More

How to create a service in angular?

September 11, 2021October 3, 2022

Services are same as class with a specific purpose of it. Services can contain method, property and any feature of an application which we can reuse multiple time in our web application. Services also contain a decorator which tell angular how to consume it in our application. Let’s create a…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version