2

I'm new in Angular 2 and I need some advices concerning my personal project. I have two components, a formation list and a form to update my datas. I work with services calls to update my datas and to display my formations list. I had a problem with the datas update on my view but I resolved it by adding a timer to refresh datas. I'm not fan of this solution because my service is called every second in my formation list interface :/

My question : What's the better way to refresh my view in my case ?

Thx in advance

amt59
  • 67
  • 4

2 Answers2

1

Your question is, how to trigger the change detection in your application. Specially when any observable/promise is fulfilled in the view, so you need to trigger the change detection in your application manually,

declare following in your component

constructor(private ref: ChangeDetectorRef)

and mark all its ancestor for check by this method,

ref.markForCheck();

in your application you can also trigger the change detection manually like given below,

ChangeDetectorRef.detectChanges() 

here is another awesome post about change detection that you can refer, which can provide you a little insight of change detection.

Stack overflow answer of change detection

Deepak Jha
  • 1,539
  • 12
  • 17
0

You should take a look at EventEmitter in Angular 2+. Since you successfully updated your data in update form, an event should be emitted to the list component. In list component, you should listen to the emitted event then refresh data by a service call. This is an example of using EventEmitter: https://developer.telerik.com/topics/web-development/component-event-binding-output-angular-2/

Hung Vi
  • 490
  • 4
  • 16