1

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?

1 Answer 1

1

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.

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

2 Comments

i already did what you told in first step .actually my adminValues Class is Model.in my service a created array adminDetails:AdminValues[] [ // save data ] .what i want is when i click my form submit button get that details and store in to this array.
You should post your code for your form template and your service then. Maybe edit your question to ask specifically what the problem is, since right now it's very broad.

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.