-1
    Like

    // My First Component
    @Component({
      selector: 'app-component1',
      templateUrl: './component1.html'
    })
    export class Component1 {
       books: any[] = ['Java'];
    }

    // My Second Component
    @Component({
      selector: 'app-component2',
      templateUrl: './component2.html'
    })
    export class Component2 {
     // Now I want to push 'Angular2' in books array from here. So, How can i do this and remember there is no any relationship of parent child component.
    }

I want to push object from Component2 to Component1.

I have already try with Observable.

Can you please help me in above scenario?

rjdkolb
  • 10,377
  • 11
  • 69
  • 89

1 Answers1

1

You can use a shared service to manage that, put provider:[myService] in the highest level module which contains these two component, then it become a singleton object.

@Injectable()
class myService{
books=[]
} 

// My First Component
@Component({
  selector: 'app-component1',
  templateUrl: './component1.html'
})
export class Component1 {
constructor(private _myService:myService){
   this._myService.boosk.push('Java')
}
}

// My Second Component
@Component({
  selector: 'app-component2',
  templateUrl: './component2.html'
})
export class Component2 {
constructor(private _myService:myService){
}
}
Fan Cheung
  • 10,745
  • 3
  • 17
  • 39