I have persons parent class and it has multiple person child class with some properties. In the home page I have a link to open the person page. When I click the link I could open the persons page with one person information by default. User can add or remove the person. When I submit the page it will redirect to home page by saving the array of data.
My problem here is, When I submit and I have added person details in it and if I click the person page again from the home page I should regain the original details by default. By I can see the same page when I have clicked before the data entry. Means every data I have added it re-initialized. I have declared the things with "static" also. Please look at the code below.
HTML code below..
<div id="personsDetails">
<person-directive *ngFor="let person of persons; let idx = index" (remove) = "removePerson(idx)">
</person-directive>
</div>
persons component is below...
@Component({
selector: 'personinvolved',
templateUrl: 'app/modules/person/personinvolved.template.html',
directives: [PersonDirective]
})
export class PersonInvolvedComponent {
private mainArray = new Array();
static persons: Array<PersonDirective> = [];
constructor( private router: Router, private _globalService:GlobalService, private el: ElementRef) {
if(PersonInvolvedComponent.persons.length == 0) {
PersonInvolvedComponent.persons.push(new PersonDirective());
}
}
get persons(){
return PersonInvolvedComponent.persons;
}
addPerson() {
PersonInvolvedComponent.persons.push(new PersonDirective());
}
removePerson(index) {
PersonInvolvedComponent.persons.splice(index,1);
}
onFormSubmit(event) {
$(this.el.nativeElement).find("person-directive").each((i,val) => {
this.mainArray[i] = new Array(10);
this.mainArray[i][0] = $(val).find("#raceSelect").val();
this.mainArray[i][1] = $(val).find("#genderSelect").val();
this.mainArray[i][2] = $(val).find("#ageSelect").val();
this.mainArray[i][3] = $(val).find("#heightSelect").val();
this.mainArray[i][4] = $(val).find("#buildSelect").val();
this.mainArray[i][5] = $(val).find("#eyeColorSelect").val();
this.mainArray[i][6] = $(val).find("#hairColorSelect").val();
this.mainArray[i][7] = $(val).find("#addInfoSelect").val();
this.mainArray[i][8] = $(val).find("#nameSelect").val();
this.mainArray[i][9] = $(val).find("#aliasNameSelect").val();
});
this._globalService.setPageData(this.mainArray,'person');
this.router.navigate(['step1']);
}
}
So by clicking submit all the data entered in person is saved in service and redirected to step1 ie home page. When I click again person link in home page, person page becomes default without having any previously entered data. Can someone please suggest me how can I maintain it back until I submit from home page.