2
Answers

how to add item from array to class using angular2

Dev Maroua

Dev Maroua

7y
189
1
Hello,
I have a user class
 
export class User {
constructor(
public email: string,
public password: string) { this.email=email;this.password=password}
}
 
I'm storing the values of this class in an array like this
 
users :User[]= [
new User('admin@admin.com','adm9'),
new User('user1@gmail.com','a23')
];
 
after I create an add_user function that allows to add a user

add_user(user:User)
{
this.users.push(the problem is here );
console.log(this.users);
this._router.navigate(['login']);
}
 
ho can i do please ?
thanks in advance 
 
 
 
       
Answers (2)
0
Dev Maroua

Dev Maroua

NA 35 1.2k 7y
@Mangesh Yes in button . I didn't understand Can you show me example
0
Mangesh Gaherwar

Mangesh Gaherwar

NA 4.1k 70.8k 7y
Dev where u are calling the AddUser function ? from the button click ?
 
if so try constructing the object as new User() and try Pushing it check if it works