0

How can I create array of class and then push some classes in it , I just have to create same as

In C#

 public class CheckBoxListItem
{        
    public string DisplayName { get; set; }
    public string Value { get; set; }
    public bool IsChecked { get; set; }         
}

List<CheckBoxListItem> cbx = new List<CheckBoxListItem>();
cbx.Add(new CheckBoxListItem(){DisplayName ="ABC",Value="NOW",IsChecked=true});

In Typescript

class Graph{
graphType:string;
checkedItems:CheckBoxListItem[];
}


 class CheckBoxListItem
 {
  DisplayName: string  
  Value:string  
  IsChecked:boolean
 }

Properties

  checksGraph :CheckBoxListItem;
  graphModel:Graph;

Constructor

constructor(private el:ElementRef, private renderer:Renderer) { 


  this.checksGraph = new CheckBoxListItem();
  this.graphModel = new Graph();
}

Problem

  pushItem(){

    this.checksGraph.DisplayName=displayName;
    this.checksGraph.Value=value;
    this.checksGraph.IsChecked=check;      
    this.graphModel.checkedItems.push(this.checksGraph); 
 }

This line could not run becuase checkedItems is undefined

this.graphModel.checkedItems.push(this.checksGraph);

also cannot use

this.graphModel.checkedItems = new CheckBoxListItem[50];

this throws TypeError: CheckBoxListItem[50] is not a constructor.

1 Answer 1

1

In your Graph class definition, change

checkedItems:CheckBoxListItem[];

to

checkedItems:CheckBoxListItem[] = [];

Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to initialize the property inside constructor after this?
when I push some class all previous elements copies the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.