Question

I'm struggling to find a way to get data from my router into my controller. The enter method on the route is correctly grabbing the data from the service, as the print method shows the JSON response in the console.

My Module looks like this:

class MyAppModule extends Module {
  MyAppModule () {
    type(UsersService);
    type(UsersController);
    type(RouteInitializerFn, implementedBy: MyAppRouteInitializer);
    factory(NgRoutingUsePushState,
        (_) => new NgRoutingUsePushState.value(false));
  }
}

and a simplified version of my router

class MyAppRouteInitializer {

  final UsersService _userService;

  MyAppRouteInitializer(this._userService);

  void call(Router router, ViewFactory views) {
     views.configure({
        'users': ngRoute(
         path: '/users',
         view: 'views/users/users.html',
         mount: {
            'list': ngRoute(
             path: '',
             view: 'views/users/list.html',
             enter: (RouteEnterEvent e) {
                _userService.all((HttpResponse response){
                    var data = response.data['body'];
                    print(data);
                });
             }
          }
        )
      });
   }
}

At the moment my controller is pretty much empty just looks like this

@NgController(
    selector: '[users-controller]',
    publishAs: 'userCtrl'
)

class UsersController {
     var list;

     UsersController() {

     }

}

Any help on how to get the service response into the controller would be awesome.

Was it helpful?

Solution

Why do you want to do this in the route enter event handler?
You don't even use any information from the route or the event.
You could just register the UserService with your Module an let it inject to your UserController.

class MyAppModule extends Module {
  MyAppModule() {
    type(UsersService);
  }
}

@NgController(
  selector: '[users-controller]',
  publishAs: 'userCtrl'
)

class UsersController {
  var list;

  UsersService _usersService;

  UsersController(this._usersService) {
    _userService.all((HttpResponse response){
                    var data = response.data['body'];
                    print(data);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top