1

I would like to to pass an array of data (data from the URL) from ParentComponent to ChildDirective with a sharedDataService. I usedthis but i don't know if it's the best way to do it.

Here is my ParentComponent

@Component({
        ...
        directives: [ChildDirective, ROUTER_DIRECTIVES],
    })
    export class FormsAddClient implements OnInit{
          params=[];
          constructor(
            private _routeParams: RouteParams,
            private _sharedData: SharedData){
             this.params['video_id']=this._routeParams.get('video_id');
             this.params['soc_id']=this._routeParams.get('soc_id');
          }
          ngOnInit(): void {
            this._sharedData.videoData.next(this.params);
          }
        }

My ChildDirective

export class ChildDirective {
  videoData;
  constructor(
        private _sharedData:SharedData) {
            this._sharedData.videoData.subscribe(data=>{
                this.videoData = data;
                console.log(this.videoData;
            });
         }
    }

And my ShareDataService

import {Injectable,Inject} from '@angular/core';
import { Subject }    from 'rxjs/Subject';
@Injectable()
export class SharedData {
  videoData = new Subject();
}

Any ideas ?

Community
  • 1
  • 1
Adrien Castagliola
  • 911
  • 2
  • 11
  • 30
  • first of all, seems like you are using an old version of angular2! i would recommend you to update to the latest final version 2.0.0! anyway.. using a service is one of the ways to go, yes. See the official docs: https://angular.io/docs/ts/latest/cookbook/component-communication.html – slaesh Sep 19 '16 at 13:38

0 Answers0