One way to solve the problem and still use components as opposed to controllers is to introduce a class that contains your shared data. In fact, this also works exactly the same with controllers, but that's a moot point because controllers seem to be going away as Randal mentioned.
(Please pardon any typos or errors - I'm typing right into the text editor, so this may not compile.)
Step 1: Introduce a shared class, annotate it as Injectable:
@Injectable()
class MySharedClass
{
String mySharedVariable;
}
Step 2: Make sure that that Angular can inject the class by telling Angular about it:
class MyInitModule extends Module {
MyInitModule() {
// Your bindings ...
// Your components, controllers are bound for Angular's DI
// This is the important part:
bind(MySharedClass);
// Typical Angular.Dart bindings:
bind(RouteInitializerFn, toValue: yourRouteInitializer);
bind(NgRoutingUsePushState, toFactory: (_) =>
new NgRoutingUsePushState.value(false));
}
main() {
applicationFactory().addModule(new MyInitModule()).run();
}
Step 3: Now add the shared class to your components' constructors. Angular will automatically inject an instance as long as you declare a requirement for it:
@Component(
selector: 'componentA',
templateUrl: 'correct_path', // insert correct value here.
publishAs: 'cmp')
class ComponentA {
MySharedClass sharedClass;
ComponentA(this.sharedClass) {
}
@Component(
selector: 'componentB',
templateUrl: 'correct_path', // insert correct value here.
publishAs: 'cmp')
class ComponentB {
MySharedClass sharedClass;
ComponentB(this.sharedClass) {
}
And now you have access to your shared class between the two (or more) components as you see fit.
(Modified per Wilson Yeung to add Injectable() annotation)