1

I have a component called "modal-form" and another called "array" and I would like to update the array after the submit of the form without refresh the view. I tried to use @Input but it didn't seem to work when I called a test function from modal component.

modal-form

Here I post the form but I refresh the view, this is not what I'm looking for.

  add() {
    this.service.post(this.form).subscribe((data) => {
      console.log(data)
      this.router.navigate([this.router.url]);
    });
  }

array

  ngOnInit(): void {
        this.service.getObjects().subscribe((data) => {
          this.objects = data;
        });
      }

1 Answer 1

2

This is the ideal situation for using a service. Either you have a local variable in a service (stuffArray for example), or you set up a Subject-as-a-Service: generally a BehaviorSubject which your array component subscribes to. Either way, you save to the array (or have the BehaviorSubject emit) when you save the form in the modal. For full safety, do so in the callback from the server request, to make sure the data has saved correctly.

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

3 Comments

How do you set the BehaviorSubject in service function ? When I set my variable I get this error : Type 'Observable<[]>' is missing the following properties from type 'BehaviorSubject<[]>
const mySubj$ = new BehaviorSubject<T>(initialValue)
You can't just assign the HTTP request as a BehaviorSubject. If you want to use that approach, I suggest you read up about it first.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.