Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

javascript - How do I persist formdata from one component to another in angular?

All I had to use was @Input, @output, routing, activated route.

There are 2 components, namely addbook and showbook. addbook component has a form with a submit button, which is supposed to save all the form data.

showbook component should just display the formdata.

These two components are placed in an a navbar of anchor tags. One is addbook anchor, which displays the form, and when you submit the form, it should persist the data (save it).

Finally, when you click on the show book anchor tag, it should display the formdata.

What I have come up with - I've used a template driven form which when submitted invokes a

function { this.route.navigate(['showbook',bookID])}

Now I have imported the path as showbook/:id and component as showbook in routerModule.forRoot([routes]), in the showbook component, I have used activatedRoute to get the params, and using this, I have displayed the form data.

RouterModule.forRoot([{path:'showbook/:id',component:showBookComponent}]) // in app module ts
this.activatedRoute.params.subscribe(params=>{
this.bookId = params['id']
})

My code displays the formdata as soon as it is submitted, though the requirement is that I should save the data, and when showbook is clicked the formdata should be displayed.

I am not allowed to use local storage.

question from:https://stackoverflow.com/questions/66047487/how-do-i-persist-formdata-from-one-component-to-another-in-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use service for this purpose.

  1. After the form is filled up in addbook and saved, store the data in service.
  2. When you navigate to showbook, retrieve the data from service.

Example:

data.service.ts

  import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class DataService {
  formData: Object;

  constructor() { }

  //assign the value
  setFormData(data){
    this.formData = data;
  }

  //To get the value
  getFormData(){
    return this.formData ;
  }
}

In addbook.component.ts

First import the service.

import { DataService } from '../../data.service';

Inject the service in your constructor

constructor(private dataService: DataService){}

On your save button function

this.dataService.setFormData(data);

In showbook.component.ts

First import the service.

import { DataService } from '../../data.service';

Inject the service in your constructor

constructor(private dataService: DataService){}
 

Display the form data on init function

 ngOnInit(): void {
    this.formData = this.dataService.getFormData();
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...