本文介绍了如何在 Angular DART 控制器之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控制器,想在它们之间发送"对象.我有这样的事情:

i have two controllers and want to "send" between them object. I have something like this:

@NgController(selector: '[users]', publishAs: 'ctrl')
class UsersController {
  List<Users> users;
}

@NgController(selector: '[user_logs]', publishAs: 'ctrl')
class LogsController {
  List<Log> logs;
  void filterLogsFor(User user) { logs = ... }
}

class MyAppModule extends Module {
  MyAppModule() {
    type(LogsController);
    type(UserController);
  }
}

我的解决方案只是将 LogsController 作为依赖项添加到 UserController 并从模板调用类似 ctrl.logsCtrl.filterLogsFor(user) 的内容.但由于某种原因它不起作用 - 我发现 DI 创建了另一个与模板本身无关的新对象 LogController - 我什至尝试更改为value(LogsController,new LogsController())",但它相同 - 它创建新的 LogsController 当新的 MyAppModule 被调用,然后我猜是新的另一个模板.我显然做错了什么 - 但文档没有帮助,angularjs 似乎根本不相似.

My solution was simply adding LogsController to UserController as dependency and calling something like ctrl.logsCtrl.filterLogsFor(user) from template. But it won't work for some reason - i found out DI create another new object LogController which is not related to template itself - i even tried change to "value(LogsController, new LogsController())", but its same - it creates new LogsController when new MyAppModule called and then new another one for template i guess. I am clearly doing something wrong - but documentation is not helpful and angularjs seems not similar at all.

更新:想象一下两个表(控制器) - 用户和日志,每个用户行都有链接来显示分配给他的日志.

UPDATE:Imagine two tables(controlers) - users and logs, every user row have link to show logs assigned to him.

推荐答案

使用最新的 AngularDart 库 (0.10.0),Günter Zöchbauer 的解决方案仍然是正确的,但语法有所改变:

With the newest AngularDart library (0.10.0), Günter Zöchbauer's solution is still correct, but the syntax has changed a bit:

// Receiver
//import 'dart:async';
String name;
Scope scope;
ReceiverConstructor(this.scope) {
  Stream mystream = scope.on('username-change');
  mystream.listen(myCallback);
}

void myCallback(ScopeEvent e) {
  this.name = e.data;
}


// Sender
scope.emit("username-change", "emit");
scope.broadcast("username-change", "broadcast");
scope.parentScope.broadcast("username-change", "parent-broadcast");
scope.rootScope.broadcast("username-change", "root-broadcast");

这篇关于如何在 Angular DART 控制器之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 21:17