angularjs - Angular 2 Observables and Change detection - How to get one component to retrieve a value, set by another component in a service? -
i think i'm overlooking fundamental, , i've been banging head couple of days now. i'm new angular , reactive programming.
so, have 1 service , 2 components.
in versionservice.ts
have helper method allows components set currentversion
in _datastore
.
private _datastore: { currentversion: }; 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 component select-version.component.ts
, through (click) event view/template, use setcurrentversion
helper method versionservice.ts
store 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 call on method in service. update: inject versionservice in both of components.
import {versionservice} '../../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 version: " + v.versionid); this.versionservice.setcurrentversion(v); }
so, in second component want retrieve value set/stored in service (_datastore
), , display in view/template (of second component)
in second-component.template.html
:
<h2>{{selectedversion}}</h2>
in second-component.ts
: update:
import {versionservice} '../../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 main component, app.component.ts:
import {sidebarcomponent} './component/sidebar/sidebar.component'; import {maincontainercomponent} './component/maincontainer/main-container.component'; @component({ selector: 'init-app', templateurl: 'app/app-component.template.html', directives: [sidebarcomponent, maincontainercomponent], }) export class appcomponent { constructor() {} }
in boot.ts:
import {bootstrap} 'angular2/platform/browser' import {appcomponent} './app.component' import {http_providers} 'angular2/http' bootstrap(appcomponent, [http_providers]) .catch(err => console.error(err));
what missing able view currentversion
in template of second component, , update each time new value set? reply!
to complement eric , pixelbits said, don't share same instance of service. 1 instance created per component since use providers
attribute @ level.
if want know more hierarchical injection, have @ answer:
hope helps you, thierry
Comments
Post a Comment