Angular Dart将数据从路由服务传递到控制器(Angular Dart passing data from route service into a controller)

我很难找到一种方法将数据从我的路由器输入到我的控制器中。 路由上的enter方法正确地从服务中获取数据,因为print方法在控制台中显示JSON响应。

我的模块看起来像这样:

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

和我的路由器的简化版本

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);
                });
             }
          }
        )
      });
   }
}
 

目前我的控制器几乎是空的,看起来像这样

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

class UsersController {
     var list;

     UsersController() {

     }

}
 

有关如何将服务响应输入控制器的任何帮助都很棒。

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.

最满意答案

为什么要在路由输入事件处理程序中执行此操作? 您甚至不使用路线或活动中的任何信息。 你可以在你的模块中注册UserService ,让它注入你的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);
  }
}

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);
  }
}

                    
                     
          

更多推荐