I think I'm overlooking something very fundamental, and I've been banging my head for a couple of days now. I'm new to Angular and reactive programming.
So, I have one service and two components.
In my versionService.ts
I have a helper method which allows components to set a currentVersion
in a _dataStore
.
private _dataStore: {
currentVersion: any
};
currVersionStream$: Subject<Version>;
constructor(public http: Http) {
this.currVersionStream$ = new BehaviorSubject<Version>(null);
this._dataStore = {
currentVersion: null
};
}
public setCurrentVersion(v: Version): void {
if (v != null) {
this._dataStore.currentVersion = v;
this.currVersionStream$.next(this._dataStore.currentVersion);
}
}
Then in my component select-version.component.ts
, through a (click) event from the view/template, I use the setCurrentVersion
helper method from the VersionService.ts
to store the cliked on version.
In select.version.template.html
:
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li *ngFor="#v of availableVersions" (click)="selectedVersion(v)">
<a href="#">
Version {{v.versionID}}
</a>
</li>
<li><a href="#">No version</a></li>
</ul>
In version.component.ts
: Here I call on the method in the service.
Update: I inject versionService in both of my components.
import {VersionService} from '../../services/services';
@Component({
selector: 'select-version',
templateUrl: 'app/component/selectVersion/select-version.template.html',
providers: [VersionService]
})
export class SelectVersionComponent implements OnInit {
constructor(public versionService: VersionService) {}
public selectedVersion(v: Version) {
console.log("SVC - set this version: " + v.versionID);
this.versionService.setCurrentVersion(v);
}
So, in my second component I want to retrieve the value set/stored in the service (_dataStore
), and display it in its view/template (of my second component)
In my second-component.template.html
:
<h2>{{selectedVersion}}</h2>
In my second-component.ts
:
Update:
import {VersionService} from '../../services/services';
@Component({
selector: 'main-container',
templateUrl: 'app/component/mainContainer/main-container.template.html',
providers: [VersionService],
})
selectedVersion: any;
constructor(public versionService: VersionService) {
this.versionService.currVersionStream$.subscribe((v) => {
if(v != null) {
this.selectedVersion = "Version" + v.versionID
}
}, error => console.error (error));
}
Update: In my main component, app.component.ts:
import {SidebarComponent} from './component/sidebar/sidebar.component';
import {MainContainerComponent} from './component/mainContainer/main-container.component';
@Component({
selector: 'init-app',
templateUrl: 'app/app-component.template.html',
directives: [SidebarComponent, MainContainerComponent],
})
export class AppComponent {
constructor() {}
}
In my boot.ts:
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {HTTP_PROVIDERS} from 'angular2/http'
bootstrap(AppComponent, [HTTP_PROVIDERS])
.catch(err => console.error(err));
What am I missing to be able to view the currentVersion
in my template of the second component, and will it update each time a new value is set?
Thanks for any reply!