1

我有一个基本的 Angular Dart 程序,当前允许登录并在登录时显示一个基本仪表板。我想做的是在成功登录后重定向到仪表板路由。我不知道如何从登录控制器中访问路由器对象,尝试使用 DI 加载Router到控制器工作中,但给了我一个新Router对象而不是之前初始化的对象(如预期的那样)。

主要.dart

import 'package:angular/angular.dart';
import 'dart:convert' show JSON;
import 'dart:html';

class TTRouter implements RouteInitializer {

  Cookies _cookies;

  TTRouter(this._cookies);

  init(Router router, ViewFactory view) {
    router.root
      ..addRoute(
          name: 'login',
          path: '/login',
          enter: view('login.partial.html'))
      ..addRoute(
          name: 'home',
          path: '/dashboard',
          enter: view('dashboard.partial.html'));
  }

}

@NgController(
    selector: '[login-controller]',
    publishAs: 'ctrl')
class LoginController {
  Http _http;
  Scope _scope;

  LoginController(this._scope, this._http);

  login() {
    // Login API request ommitted
    // TODO: insert redirect to 'home' route here
  }
}


class TTModule extends Module {
  TTModule() {
    type(RouteInitializer, implementedBy: TTRouter);
    type(LoginController);

    factory(NgRoutingUsePushState,
        (_) => new NgRoutingUsePushState.value(false));
  }
}

main() => ngBootstrap(module: new TTModule());

login()ng-submit="ctrl.login()从登录局部视图调用使用。

如果我以错误的方式处理此问题,我也将感谢对代码结构的任何评论。我是 Dart 和 Angular 的新手(阅读/观看教程,但这是我自己构建的第一个应用程序)。

4

3 回答 3

3

如果您将路由器添加value到模块而不是type每次都获得相同的实例。

TTModule() {
  value(RouteInitializer, new TTRouter());
}
于 2014-01-18T22:19:49.477 回答
1

尝试使用 NgRoutingHelper。

class LoginController {
  Http _http;
  Scope _scope;

  NgRoutingHelper locationService;
  LoginController(this._scope, this._http, NgRoutingHelper this.locationService );

  login() {
    // Login API request ommitted
    // TODO: insert redirect to 'home' route here
    locationService.router.go('home', {} );
  }
}

不要忘记在你的模块中添加服务

type(NgRoutingHelper );
于 2014-01-20T17:07:16.603 回答
1

你应该总是得到相同的实例Router——只能有一个,否则不可预测的事情就会开始发生。

class LoginController {
  Http _http;
  Scope _scope;
  Router _router;

  LoginController(this._scope, this._http, this._router);

  login() {
    _router.go('home', {} );
  }
}
于 2014-01-21T20:39:56.547 回答