Z
Z
zagamay_ru2016-01-11 11:06:41
Angular
zagamay_ru, 2016-01-11 11:06:41

How to pass data from one component to another in Angular2?

Hello!
I'm trying the second version of Angular.
Question about inheritance, is there a main component to display the template

export class App {
  name = 'Angular 2 Webpack Starter';
  url = 'https://twitter.com/AngularClass';
  guest = true;

  constructor() {

  }

  isGuest(login) {
    return this.guest = login;
  }
}

Other components are processed in it through a router, for example, the {About} component, how can I call a property in this component from the parent {App} component?
I thought that it would be right to declare it this way:
class About extends App {}
But I swore at such a design ..
Help me figure it out.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
ivanesi, 2016-01-14
@zagamay_ru

can be passed in a service that is defined in the parent component.

//parent
@Component({
  //...
  providers: [SharedDataService] 
  //...
})

https://angular.io/docs/ts/latest/guide/hierarchic...

_
_ _, 2016-01-11
@AMar4enko

In the first Angular, this was done through directive controllers or services.
In the second, through services or hierarchical Dependency Injection - you can send the necessary data from the parent component to the child component, look for the dock on the offsite

M
Mikhail Osher, 2016-01-11
@miraage

Create a service, make it @Injectable.
Further, it should be clear. :)

R
rinatoptimus, 2016-01-11
@rinatoptimus

Try the construct:

import {название_родительского_компонента} from './родительский_компонент';
...
@Component({
  ...
  directives: [название_родительского_компонента]
})

Z
zagamay_ru, 2016-01-11
@zagamay_ru

I tried it through a directive, I got: Unexpected directive value 'undefined' on the View of component.
Tried via @Injectable , works but doesn't inherit, changing property in parent, property with child component doesn't change.. doing something wrong

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question