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
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 ?
CodePudding user response:
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);