2

I have got 2 components, let's say, Component A is a list view and Component B is a details view. Each row from the list view is clickable and will redirect to Component B upon clicking.

Component B allows editing and saving the details. I have added a Back button to Component B to allow me to go back to the list view.

But the problem I am having is that I can't see the updated list view and have to manually refresh the browser, and then I can see the updated list there.

I have tried directly using window.location and it works but really I don't prefer this approach.

public back() {
   window.location.assign('/listview');
}

I wonder if there's any better way to solve this problem?

Update:

public onSelected(model: MyModel) {
    const detailsViewUrl = `/detailsview/${model.id}`;
    this._router.navigateByUrl(detailsViewUrl );
  }
halfer
  • 19,824
  • 17
  • 99
  • 186
woodykiddy
  • 6,074
  • 16
  • 59
  • 100
  • 2
    Be careful with your words : is `A` the *parent* of `B`, or are the two components routed ? –  May 15 '18 at 09:42
  • How is the data communicated between component A and B? – Joshua Chan May 15 '18 at 09:56
  • @JoshuaChan I've updated the question to show how ID gets passed to the details view, i.e. component `B` – woodykiddy May 15 '18 at 10:14
  • just called the list view again internally and hit db so updated values will be displayed the list view – Karthic G May 15 '18 at 11:19
  • this is not angularjs, I'm working on very large scale angular application for the past year and not once did I need to trigger detection manually. if change detection isn't triggering you're probably not doing stuff the 'angular way' - please post a live example, it'll be easy to spot the issue. – Stavm May 15 '18 at 12:57

5 Answers5

1

You can just emit an @Output EventEmitter with a method on Parent that looks in the event for a change with a variable stored in the component like this:

@Output someOutput: EventEmitter = new Event Emitter<any>;

HTML:

  <b-component (someOutput)=getOutput($event)></b-component>

AComponent:

 getOut(event){
     let output = event;
     if(this.something != output){
          this.ngOnDestroy(); // or something that you can use to make it
    }

That should work as intended.

Rancio
  • 155
  • 1
  • 3
  • 15
0

It sounds like this is an issue with Angular's change detection when changing the contents of an array. See here:

Angular 2: How to detect changes in an array? (@input property)

The solutions in this questions should work but an easy way I have used in the past to force changes in an array to be recognised by Angular is to reassign the array after making the changes:

myArray = [...myArray];
Plog
  • 9,164
  • 5
  • 41
  • 66
0

use following routing fuction on back button click

public back() {
   this._router.navigateByUrl('/listview')
}
or
public back() {
   this._router.navigate('/listview')
}
mittal bhatt
  • 955
  • 6
  • 13
0

Try this,

Just called the list view again internally and hit db at same time so updated values will be displayed in the list view.

calling the route by using below:

this.router.navigate(['/listview']);
Karthic G
  • 1,162
  • 1
  • 14
  • 31
0

Seems like a change detection issue, there are some ways to manually trigger change detection like so:

  1. Inject ChangeDetectorRef.

  2. Call it when you go back like so:

    public back() {
       ChangeDetectorRef.detectChanges()
    }
    

Refer to this: Triggering change detection manually in Angular

hyper0009
  • 236
  • 1
  • 11