0

I have created a local instance of the service and accessing data from it directly. Is this incorrect if it is a simple use case (sharing data between components). I am trying to grasp some core concepts of ng2 and I feel like there may be a better option.

eg

export class FormTwoComponent implements OnInit {
    private model: WinnerModel;

    constructor(private formService: FormService, private router: Router) {}

    ngOnInit() {
        this.model = this.formService.winnerModel;
    }

    onSubmit() {
        this.formService.winnerModel = this.model;
        this.router.navigate(['/form/3'])
    }
}

Thanks

1
  • can you elaborate your question? do you want to access data of one service into another service or you want it to access in controller? Both have their own separate ways to do data transfer. Commented Jul 8, 2016 at 20:45

1 Answer 1

1

Angular has a wonderful page about component interactions. One thing that I feel isn't made too abundantly clear is that if you don't make references to objects, then your data in your component will get left behind. For example:

component.variable = service.variable //this will take the current value of what the service variable is, if the service variable ever changes then the component will not get updated.
component.object = service.object //this will however create a link and any updates or changes made to the service object, then the component reference will be notified and updated.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.