4

i have an Agent class

export class Agent{
    ID:number;
    Name: string;
}

And in component i have a variable of this class array

public agents : Agent[];

Now how can i push values in this variable? I have tried following methods but no one worked.

agents.push({"ID":1,"Name":"Abc"});
agents.push(new Agent(){"ID":1,"Name":"Abc"});
1

4 Answers 4

1

I think, merely change:

public agents : Agent[]; to public agents : Agent[]= []

and (if you still in the same component and inside a method) :

agents.push({"ID":1,"Name":"Abc"}); to this.agents.push({"ID":1,"Name":"Abc"});


Update:

If you're within the same method:

someMethod(){
  let localAgents :Agent[]= []; 
  localAgents.push({ID:1,Name:"Abc"});
}

If you want to push while declaring, that cannot be done, you merely initialize with default data, why would you push ?

E.g.

  public agents : Agent[] = [{ID:1,Name:"Abc"}];

DEMO

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

10 Comments

so you are pushing values in constructor and i am pushing values when i initialise variable. And there "this" keyword cannot used. Anyhow it worked in constructor.
@HunzlaSheikh a constructor or whatever method, doesnt matter. Share your code, if u want a more specific answer. And my answer and yours are not the same. E.g. U cannot not push in an uninitialized array (as u did)
export class LeafletmapComponent implements OnInit { public agents :Agent[]; agents.push({ID:1,Name:"Abc",Latitude:2323,Longitude:4234}); constructor() {} }
@HunzlaSheikh still, u cannot push in an uninitialized array.
@HunzlaSheikh, you cannot perform an action (push, unshift..) unless you're wrapped by a method braces, that's how angular works !
|
1
agents.push({ ID:1, Name:"Abc" } as Agent);

or

const _agent = new Agent();
_agent.ID = 1;
_agent.Name = "ABC";

agents.push(_agent);

3 Comments

not working. facing this problem "Subsequent property declarations must have the same type"
when using which approach ? you should initialize your array first = []
for both approaches
0

You need to initialize your array, otherwise you'll get a "cannot read property 'push' of undefined" error. Using a plain object like you did is fine (it's good practice to remove the quotes around the props), e.g.

let agents : Agent[] = [];
agents.push({ ID: 1, Name: "Abc"});
console.log(agents); // outputs: [ { ID: 1, Name: 'Abc' } ]

If you actually want to create an instance of the class, you can do it like this:

const agent = new Agent();
agent.ID = 1;
agent.Name = "ABC";
agents.push(agent);

5 Comments

not working. facing this problem "Subsequent property declarations must have the same type"
{ "compileOnSave": false, "compilerOptions": { "baseUrl": "./", "outDir": "./dist/out-tsc", "sourceMap": true, "declaration": false, "downlevelIteration": true, "experimentalDecorators": true, "module": "esnext", "moduleResolution": "node", "importHelpers": true, "target": "es2015", "typeRoots": [ "node_modules/@types" ], "lib": [ "es2018", "dom" ] }, "angularCompilerOptions": { "fullTemplateTypeCheck": true, "strictInjectionParameters": true } }
Show us how you initialize it the agents array, please.
public agents : Agent[]; //declaring variable agents.push({"ID":1,"Name":"Abc"}); //pushing values
@HunzlaSheikh: That's the declaration. Where do you initialize the array, i.e. agents = [] ?
0

First, your class needs a constructor in order to initialize it with params

export class Agent {
    ID: number;
    Name: string;

    constructor(id: number, name: string) {
        this.ID = id;
        this.Name = name;
    }
}

Then, in your component

...
public agents : Agent[];
...
pushAgent(id: number, name: string) {
    this.agents = [new Agent(id, name)]
}
  • Keep in mind, if you want Angular to display the content of the agents, you must change the array by reference. Otherwise angular would not detect the changes.

Hope this helps!

Comments

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.