0

I am using Angular 2.2.3. If an object is stored in a service and the object is retrieved from the service in a component via getter and set to local object. If changes are made on the component should that also affect the service object?

Why I ask is in my project it does do the above but I thought Angular 2 was designed so that component/services are isolated. Hence why we use RXJS to broadcast changes.

Here is a example of what I mean:

My service has the following:

test: {name: string, test: boolean} = {name:'Peter',test:false};
getTest() {
    return this.test;
}

My Component has the following function:

test() {
    let test: any = this.myService.getTest();
    test.name = 'John';
    test.test = true;
    console.log('Component Object',test, 'Service Component', this.myService.getTest());
  }

Console Prints the following when the above function is executed in the component.

Component Object Object {name: "John", test: true} Service Component Object {name: "John", test: true}
Ka Tech
  • 8,937
  • 14
  • 53
  • 78

0 Answers0