Roka545
Roka545

Reputation: 3636

Get value from one component and use it in another.

I have two component files in my Angular 2 app - home.ts and folder-selector.ts. I have a variable inside folder-selector.ts called pathToNode. How do I access this value from home.ts? My home component has an import for FolderSelectorService - I originally thought that maybe I would need to create a service call to reference pathToNode but that doesn't seem right. Basically how do I get component values between components? Am I supposed to have an import statement for the folder-selector component?

Upvotes: 0

Views: 257

Answers (1)

Klaas Cuvelier
Klaas Cuvelier

Reputation: 301

Passing data to (child) components should be done through @Input, read https://toddmotto.com/passing-data-angular-2-components-input and https://angular.io/docs/ts/latest/cookbook/component-communication.html

It's important to get familiar with data flows in Angular 2 to create apps, data flows down through @Input, events flow up through @Output

You can only pass data down in your component tree, so this requires for your home component to be nested inside folder-selector. (If that is not the case, you'd have to create a parent component which holds the data)

<folder-selector>
    <home [pathToNode]="pathToNode"> ...</home>
</folder-selector

Upvotes: 1

Related Questions