NicoleZ
NicoleZ

Reputation: 1780

Get Values to the Array Angular 2

I have Admin.ts Class

export class AdminValues{

    nameAd:String;
    email_addr:String;
    passwrd:String;

    constructor(adminName:String,adminEmail:String,adminPassword:String){

        this.nameAd=adminName;
        this.email_addr=adminEmail;
        this.passwrd=adminPassword;
    }

} 

I am completely new to Angular 2. I have created an admin form (AdminForm Component) to get input. I want create an Angular 2 service to add new admins to the Admin Type Array. How should I do that?

Upvotes: 1

Views: 1006

Answers (1)

Adam
Adam

Reputation: 4683

This is a pretty broad questions, since there are an infinite number of ways to do this. This article will give you a pretty good overview on getting started.

Most simply, you need to make a new file, with this content in it:

@Injectable()
export class AdminService {
  getArray() {
    return ['my array'];
  }
}

You will need to include this service in your app modules in the providers section, and import it into your AdminForm component. Something along the lines of import { AdminService } from './AdminService'; should do the job.

You then should change your constructor to this:

constructor(private adminService: AdminService, adminName:String,adminEmail:String,adminPassword:String){

    this.nameAd=adminName;
    this.email_addr=adminEmail;
    this.passwrd=adminPassword;
}

Which will then give you access to your admin service and you can get your array.

myArray;

constructor(private adminService: AdminService, adminName:String,adminEmail:String,adminPassword:String){

    this.nameAd=adminName;
    this.email_addr=adminEmail;
    this.passwrd=adminPassword;
    this.myArray = adminService.getArray();
}

This answer should get you started, but if you need more in-depth information about services, the link at the top of this answer should have all the information you need to get going.

Upvotes: 1

Related Questions