问题描述
我有两个控制器,并希望在它们之间发送对象。我有这样的:
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);
}
}
$ b $ p我的解决方案只是将LogsController添加到UserController从模板调用类似ctrl.logsCtrl.filterLogsFor(user)。但它不会工作的某些原因 - 我发现DI创建另一个新的对象LogController它不与模板本身相关 - 我甚至尝试更改为值(LogsController,新的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ünterZö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控制器之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!