angular 5 before route from one component to another how to save component form data
I am trying to route one component to another; each component has some form data (this.formBuilder.group
). while I navigate from one component to another, if I come back to the previous component, the filled data is not restored.
Is that any way to save the data before routing for each component? Is there any lifecycle method is there to detect before leaving the component to save all form data?
navigation.ts:
if(currentNav == "Personal Details"){
this.router.navigateByUrl('/understand/personal');
} else if(currentNav == "Contact Details"){
this.router.navigateByUrl('/understand/contact');
}
contact.ts
ngOnInit() {
this.contactDetails = this.formBuilder.group({
email: ['', [Validators.required, Validators.email]],
mobileNumber: ['', Validators.required],
secondaryNumber: ['', Validators.required]
});
}
personal.ts
this.personalDetails = this.formBuilder.group({
country: ['', Validators.required],
countryOfIssue: ['', Validators.required],
countryOfBirth: ['', Validators.required]
});
}
angular angular5
add a comment |
I am trying to route one component to another; each component has some form data (this.formBuilder.group
). while I navigate from one component to another, if I come back to the previous component, the filled data is not restored.
Is that any way to save the data before routing for each component? Is there any lifecycle method is there to detect before leaving the component to save all form data?
navigation.ts:
if(currentNav == "Personal Details"){
this.router.navigateByUrl('/understand/personal');
} else if(currentNav == "Contact Details"){
this.router.navigateByUrl('/understand/contact');
}
contact.ts
ngOnInit() {
this.contactDetails = this.formBuilder.group({
email: ['', [Validators.required, Validators.email]],
mobileNumber: ['', Validators.required],
secondaryNumber: ['', Validators.required]
});
}
personal.ts
this.personalDetails = this.formBuilder.group({
country: ['', Validators.required],
countryOfIssue: ['', Validators.required],
countryOfBirth: ['', Validators.required]
});
}
angular angular5
visit this url for routing events angular.io/api/router/Event You need to store your form data in temp memory. when you come back then you need to read stored data and re-assign to form.
– Hardik
Nov 12 '18 at 4:27
add a comment |
I am trying to route one component to another; each component has some form data (this.formBuilder.group
). while I navigate from one component to another, if I come back to the previous component, the filled data is not restored.
Is that any way to save the data before routing for each component? Is there any lifecycle method is there to detect before leaving the component to save all form data?
navigation.ts:
if(currentNav == "Personal Details"){
this.router.navigateByUrl('/understand/personal');
} else if(currentNav == "Contact Details"){
this.router.navigateByUrl('/understand/contact');
}
contact.ts
ngOnInit() {
this.contactDetails = this.formBuilder.group({
email: ['', [Validators.required, Validators.email]],
mobileNumber: ['', Validators.required],
secondaryNumber: ['', Validators.required]
});
}
personal.ts
this.personalDetails = this.formBuilder.group({
country: ['', Validators.required],
countryOfIssue: ['', Validators.required],
countryOfBirth: ['', Validators.required]
});
}
angular angular5
I am trying to route one component to another; each component has some form data (this.formBuilder.group
). while I navigate from one component to another, if I come back to the previous component, the filled data is not restored.
Is that any way to save the data before routing for each component? Is there any lifecycle method is there to detect before leaving the component to save all form data?
navigation.ts:
if(currentNav == "Personal Details"){
this.router.navigateByUrl('/understand/personal');
} else if(currentNav == "Contact Details"){
this.router.navigateByUrl('/understand/contact');
}
contact.ts
ngOnInit() {
this.contactDetails = this.formBuilder.group({
email: ['', [Validators.required, Validators.email]],
mobileNumber: ['', Validators.required],
secondaryNumber: ['', Validators.required]
});
}
personal.ts
this.personalDetails = this.formBuilder.group({
country: ['', Validators.required],
countryOfIssue: ['', Validators.required],
countryOfBirth: ['', Validators.required]
});
}
angular angular5
angular angular5
edited Nov 13 '18 at 4:01
Joel
1,5726719
1,5726719
asked Nov 12 '18 at 3:45
Krish
2092415
2092415
visit this url for routing events angular.io/api/router/Event You need to store your form data in temp memory. when you come back then you need to read stored data and re-assign to form.
– Hardik
Nov 12 '18 at 4:27
add a comment |
visit this url for routing events angular.io/api/router/Event You need to store your form data in temp memory. when you come back then you need to read stored data and re-assign to form.
– Hardik
Nov 12 '18 at 4:27
visit this url for routing events angular.io/api/router/Event You need to store your form data in temp memory. when you come back then you need to read stored data and re-assign to form.
– Hardik
Nov 12 '18 at 4:27
visit this url for routing events angular.io/api/router/Event You need to store your form data in temp memory. when you come back then you need to read stored data and re-assign to form.
– Hardik
Nov 12 '18 at 4:27
add a comment |
2 Answers
2
active
oldest
votes
Persist the data captured in each component in a global service and restore the same when moving back and forth. You can assign the global service object in your constructor or ngOnInit method. Something like below:
home.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { GlobalService } from'../global.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit, OnDestroy {
homeDetails: FormGroup;
constructor(private formBuilder: FormBuilder,private globalService: GlobalService) {
this.homeDetails = this.formBuilder.group({
homename:['', Validators.required]
});
}
ngOnInit() {
if(this.globalService.homeDetails){
this.homeDetails = this.globalService.homeDetails;
}
}
ngOnDestroy() {
this.globalService.homeDetails = this.homeDetails;
}
}
global.service.ts
import { FormGroup } from '@angular/forms';
import { Injectable } from '@angular/core';
@Injectable()
export class GlobalService {
public homeDetails: FormGroup;
public aboutDetails: FormGroup;
}
You can look at OnDestroy to implement any code before moving to another component.
Updated: Code for home.component.ts and global.service.ts as per the stackblitz url
can u able to share any fiddle example ?
– Krish
Nov 12 '18 at 4:56
user3392782: - here i try to create the service, please help to update stackblitz.com/edit/…
– Krish
Nov 12 '18 at 7:12
1
I am unable to save the stackblitz edit. Have updated my answer with the changes needed.
– user3392782
Nov 13 '18 at 0:54
wow. it work exactly what i expected... thanks a lot, please find my updated sackblits....stackblitz.com/edit/angular-router-example-nufsnz?file=app/…
– Krish
Nov 18 '18 at 2:08
1
You will have to pull the required data from global service and construct the respective request to post. Its pretty standard. Not sure what help you would need.
– user3392782
Nov 19 '18 at 2:47
|
show 2 more comments
Instead of lifecycle methods, you can probably decide to use CanDeactivate Route Guard on exiting component, which give you ability to add features such as save and cancel. For info try https://v5.angular.io/guide/router#candeactivate-handling-unsaved-changes.
hi, can you help to update the same in this stack? stackblitz.com/edit/angular-router-example-c92hmv?file=app/…
– Krish
Nov 12 '18 at 7:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255701%2fangular-5-before-route-from-one-component-to-another-how-to-save-component-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Persist the data captured in each component in a global service and restore the same when moving back and forth. You can assign the global service object in your constructor or ngOnInit method. Something like below:
home.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { GlobalService } from'../global.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit, OnDestroy {
homeDetails: FormGroup;
constructor(private formBuilder: FormBuilder,private globalService: GlobalService) {
this.homeDetails = this.formBuilder.group({
homename:['', Validators.required]
});
}
ngOnInit() {
if(this.globalService.homeDetails){
this.homeDetails = this.globalService.homeDetails;
}
}
ngOnDestroy() {
this.globalService.homeDetails = this.homeDetails;
}
}
global.service.ts
import { FormGroup } from '@angular/forms';
import { Injectable } from '@angular/core';
@Injectable()
export class GlobalService {
public homeDetails: FormGroup;
public aboutDetails: FormGroup;
}
You can look at OnDestroy to implement any code before moving to another component.
Updated: Code for home.component.ts and global.service.ts as per the stackblitz url
can u able to share any fiddle example ?
– Krish
Nov 12 '18 at 4:56
user3392782: - here i try to create the service, please help to update stackblitz.com/edit/…
– Krish
Nov 12 '18 at 7:12
1
I am unable to save the stackblitz edit. Have updated my answer with the changes needed.
– user3392782
Nov 13 '18 at 0:54
wow. it work exactly what i expected... thanks a lot, please find my updated sackblits....stackblitz.com/edit/angular-router-example-nufsnz?file=app/…
– Krish
Nov 18 '18 at 2:08
1
You will have to pull the required data from global service and construct the respective request to post. Its pretty standard. Not sure what help you would need.
– user3392782
Nov 19 '18 at 2:47
|
show 2 more comments
Persist the data captured in each component in a global service and restore the same when moving back and forth. You can assign the global service object in your constructor or ngOnInit method. Something like below:
home.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { GlobalService } from'../global.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit, OnDestroy {
homeDetails: FormGroup;
constructor(private formBuilder: FormBuilder,private globalService: GlobalService) {
this.homeDetails = this.formBuilder.group({
homename:['', Validators.required]
});
}
ngOnInit() {
if(this.globalService.homeDetails){
this.homeDetails = this.globalService.homeDetails;
}
}
ngOnDestroy() {
this.globalService.homeDetails = this.homeDetails;
}
}
global.service.ts
import { FormGroup } from '@angular/forms';
import { Injectable } from '@angular/core';
@Injectable()
export class GlobalService {
public homeDetails: FormGroup;
public aboutDetails: FormGroup;
}
You can look at OnDestroy to implement any code before moving to another component.
Updated: Code for home.component.ts and global.service.ts as per the stackblitz url
can u able to share any fiddle example ?
– Krish
Nov 12 '18 at 4:56
user3392782: - here i try to create the service, please help to update stackblitz.com/edit/…
– Krish
Nov 12 '18 at 7:12
1
I am unable to save the stackblitz edit. Have updated my answer with the changes needed.
– user3392782
Nov 13 '18 at 0:54
wow. it work exactly what i expected... thanks a lot, please find my updated sackblits....stackblitz.com/edit/angular-router-example-nufsnz?file=app/…
– Krish
Nov 18 '18 at 2:08
1
You will have to pull the required data from global service and construct the respective request to post. Its pretty standard. Not sure what help you would need.
– user3392782
Nov 19 '18 at 2:47
|
show 2 more comments
Persist the data captured in each component in a global service and restore the same when moving back and forth. You can assign the global service object in your constructor or ngOnInit method. Something like below:
home.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { GlobalService } from'../global.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit, OnDestroy {
homeDetails: FormGroup;
constructor(private formBuilder: FormBuilder,private globalService: GlobalService) {
this.homeDetails = this.formBuilder.group({
homename:['', Validators.required]
});
}
ngOnInit() {
if(this.globalService.homeDetails){
this.homeDetails = this.globalService.homeDetails;
}
}
ngOnDestroy() {
this.globalService.homeDetails = this.homeDetails;
}
}
global.service.ts
import { FormGroup } from '@angular/forms';
import { Injectable } from '@angular/core';
@Injectable()
export class GlobalService {
public homeDetails: FormGroup;
public aboutDetails: FormGroup;
}
You can look at OnDestroy to implement any code before moving to another component.
Updated: Code for home.component.ts and global.service.ts as per the stackblitz url
Persist the data captured in each component in a global service and restore the same when moving back and forth. You can assign the global service object in your constructor or ngOnInit method. Something like below:
home.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { GlobalService } from'../global.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit, OnDestroy {
homeDetails: FormGroup;
constructor(private formBuilder: FormBuilder,private globalService: GlobalService) {
this.homeDetails = this.formBuilder.group({
homename:['', Validators.required]
});
}
ngOnInit() {
if(this.globalService.homeDetails){
this.homeDetails = this.globalService.homeDetails;
}
}
ngOnDestroy() {
this.globalService.homeDetails = this.homeDetails;
}
}
global.service.ts
import { FormGroup } from '@angular/forms';
import { Injectable } from '@angular/core';
@Injectable()
export class GlobalService {
public homeDetails: FormGroup;
public aboutDetails: FormGroup;
}
You can look at OnDestroy to implement any code before moving to another component.
Updated: Code for home.component.ts and global.service.ts as per the stackblitz url
edited Nov 13 '18 at 0:58
answered Nov 12 '18 at 4:32
user3392782
1614
1614
can u able to share any fiddle example ?
– Krish
Nov 12 '18 at 4:56
user3392782: - here i try to create the service, please help to update stackblitz.com/edit/…
– Krish
Nov 12 '18 at 7:12
1
I am unable to save the stackblitz edit. Have updated my answer with the changes needed.
– user3392782
Nov 13 '18 at 0:54
wow. it work exactly what i expected... thanks a lot, please find my updated sackblits....stackblitz.com/edit/angular-router-example-nufsnz?file=app/…
– Krish
Nov 18 '18 at 2:08
1
You will have to pull the required data from global service and construct the respective request to post. Its pretty standard. Not sure what help you would need.
– user3392782
Nov 19 '18 at 2:47
|
show 2 more comments
can u able to share any fiddle example ?
– Krish
Nov 12 '18 at 4:56
user3392782: - here i try to create the service, please help to update stackblitz.com/edit/…
– Krish
Nov 12 '18 at 7:12
1
I am unable to save the stackblitz edit. Have updated my answer with the changes needed.
– user3392782
Nov 13 '18 at 0:54
wow. it work exactly what i expected... thanks a lot, please find my updated sackblits....stackblitz.com/edit/angular-router-example-nufsnz?file=app/…
– Krish
Nov 18 '18 at 2:08
1
You will have to pull the required data from global service and construct the respective request to post. Its pretty standard. Not sure what help you would need.
– user3392782
Nov 19 '18 at 2:47
can u able to share any fiddle example ?
– Krish
Nov 12 '18 at 4:56
can u able to share any fiddle example ?
– Krish
Nov 12 '18 at 4:56
user3392782: - here i try to create the service, please help to update stackblitz.com/edit/…
– Krish
Nov 12 '18 at 7:12
user3392782: - here i try to create the service, please help to update stackblitz.com/edit/…
– Krish
Nov 12 '18 at 7:12
1
1
I am unable to save the stackblitz edit. Have updated my answer with the changes needed.
– user3392782
Nov 13 '18 at 0:54
I am unable to save the stackblitz edit. Have updated my answer with the changes needed.
– user3392782
Nov 13 '18 at 0:54
wow. it work exactly what i expected... thanks a lot, please find my updated sackblits....stackblitz.com/edit/angular-router-example-nufsnz?file=app/…
– Krish
Nov 18 '18 at 2:08
wow. it work exactly what i expected... thanks a lot, please find my updated sackblits....stackblitz.com/edit/angular-router-example-nufsnz?file=app/…
– Krish
Nov 18 '18 at 2:08
1
1
You will have to pull the required data from global service and construct the respective request to post. Its pretty standard. Not sure what help you would need.
– user3392782
Nov 19 '18 at 2:47
You will have to pull the required data from global service and construct the respective request to post. Its pretty standard. Not sure what help you would need.
– user3392782
Nov 19 '18 at 2:47
|
show 2 more comments
Instead of lifecycle methods, you can probably decide to use CanDeactivate Route Guard on exiting component, which give you ability to add features such as save and cancel. For info try https://v5.angular.io/guide/router#candeactivate-handling-unsaved-changes.
hi, can you help to update the same in this stack? stackblitz.com/edit/angular-router-example-c92hmv?file=app/…
– Krish
Nov 12 '18 at 7:15
add a comment |
Instead of lifecycle methods, you can probably decide to use CanDeactivate Route Guard on exiting component, which give you ability to add features such as save and cancel. For info try https://v5.angular.io/guide/router#candeactivate-handling-unsaved-changes.
hi, can you help to update the same in this stack? stackblitz.com/edit/angular-router-example-c92hmv?file=app/…
– Krish
Nov 12 '18 at 7:15
add a comment |
Instead of lifecycle methods, you can probably decide to use CanDeactivate Route Guard on exiting component, which give you ability to add features such as save and cancel. For info try https://v5.angular.io/guide/router#candeactivate-handling-unsaved-changes.
Instead of lifecycle methods, you can probably decide to use CanDeactivate Route Guard on exiting component, which give you ability to add features such as save and cancel. For info try https://v5.angular.io/guide/router#candeactivate-handling-unsaved-changes.
answered Nov 12 '18 at 5:18
KiraAG
304110
304110
hi, can you help to update the same in this stack? stackblitz.com/edit/angular-router-example-c92hmv?file=app/…
– Krish
Nov 12 '18 at 7:15
add a comment |
hi, can you help to update the same in this stack? stackblitz.com/edit/angular-router-example-c92hmv?file=app/…
– Krish
Nov 12 '18 at 7:15
hi, can you help to update the same in this stack? stackblitz.com/edit/angular-router-example-c92hmv?file=app/…
– Krish
Nov 12 '18 at 7:15
hi, can you help to update the same in this stack? stackblitz.com/edit/angular-router-example-c92hmv?file=app/…
– Krish
Nov 12 '18 at 7:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255701%2fangular-5-before-route-from-one-component-to-another-how-to-save-component-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
visit this url for routing events angular.io/api/router/Event You need to store your form data in temp memory. when you come back then you need to read stored data and re-assign to form.
– Hardik
Nov 12 '18 at 4:27