Roger Chan Roger Chan - 6 months ago 38
Dart Question

How to fetch data before navigating in AngularDart?

I want to fetch all data from remote service at once before routing to Profile.

I find typescript version angular have Resolve to help to fetch data before navigating. But I cannot find any example in Dart version ( angular2: ^3.0.0).

@RouteConfig(const [
const Route(path: '/', name: 'Home', component: HomeComponent, useAsDefault: true]),
const Route(path: '/profile', name: 'Profile', component: ProfileComponent),])
class AppComponent implements OnInit{


Anyone know how to handle it by using Dart?

Answer Source

The current router in Angular.dart doesn't have Resolve.
Depending on what exactly you try to accomplish, there might be different workarounds.

Usually just wrapping the content of the components template with

<template [ngIf]="data != null"> ... </template>

should do, and then assign to data when it's available and the component will be shown.

Another more generic way might be to create a custom MyRouterOutlet that might extend RouterOutlet and customize it so that it delays adding the component until an event was received.

Add to the RouterOutlet

dataArrived() {
  // call the code that actually adds the component

then use it like

<router-outlet fetchDataDirective></router-outlet>

and a directive like

@Directive(selector: '[fetchDataDirective]')
class FetchDataDirective implements OnInit {
  final DataService dataService;

  get dataArrived =>; 
  final _dataArrived = new StreamController(); 

  ngOnInit() async {
    await dataService.fetchData();

that emits the event the RouterOutlet waits for.

It was also mentioned that a new router for Angular.dart is work in progress (might still take a while though).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download