Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add static data to a typescript model class

I have a requirement to create a model class in typescript, this is my data.

A-E
  Automative
  Chemicals
  Energy

F-O
 Forest Product
 Industrial
 Mining

P-Z
 Retail
 Software Platform
 Unilites

For this data, I have created below model class

export class Industries 
{
  alphaIndex: string = '';
  industries: Array<string> = [];

  constructor(alphaIndex: string, industries: []) {
    this.alphaIndex = alphaIndex;
    this.industries = industries; 
  }
}

When I want to add data in it, getting error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let record = new Industries ({alphaIndex: 'A-E', industries: ['Automative','Chemicals']});
this.industries.push(record);

Getting below below error

Expected 2 arguments, but got 1.ts(2554) home.component.ts(28, 35): An argument for 'industries' was not provided

This is correct model for provided data ? How can I add data to my model ?

>Solution :

You are passing in an object into the constructor

Instead you should pass as separate variables

let record = new Industries ('A-E',['Automative','Chemicals']);
this.industries.push(record);

if you wanted to have it as an object type param then you would need to do this


export interface IIndustryParam {
   alphaIndex : string;
   industries: Array<string>;
}

export class Industries 
{
  alphaIndex: string = '';
  industries: Array<string> = [];

  constructor(industryParam : IIndustryParam) {
    this.alphaIndex = industryParam.alphaIndex;
    this.industries = industryParam.industries; 
  }
}

let record = new Industries ({alphaIndex: 'A-E', industries: ['Automative','Chemicals']});
this.industries.push(record);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading