问题描述
我正在努力寻找一种方法将数据从我的路由器获取到我的控制器中.路由上的 enter 方法正确地从服务中获取数据,因为打印方法在控制台中显示了 JSON 响应.
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.
我的模块如下所示:
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);
});
}
}
)
});
}
}
目前我的控制器几乎是空的,看起来像这样
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
.
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);
}
}
这篇关于Angular Dart 将数据从路由服务传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!