I have a property in a top level Component that is used data from a HTTP source like so (this is in a file called app.ts):
import {UserData} from './services/user-data/UserData';
Component({
selector: 'app', // <app></app>
providers: [...FORM_PROVIDERS],
directives: [...ROUTER_DIRECTIVES],
pipes: [],
template: require('./app.html')
})
@RouteConfig([
// stuff here
])
export class App {
// Please note that UserData is an Injectable Service I have written
userStatus: UserStatus;
constructor(private userData: UserData) {
this.userStatus = new UserStatus();
}
ngOnInit() {
this.userData.getUserStatus()
.subscribe(
(status) => {
this.userStatus = status; // I want to access this in my Child Components...
},
(err) => {console.log(err);},
() => {console.log("User status complete"); }
);
}
}
Now, I have another Component that is a direct child of the top level Component and within it I would like to access the parent's property 'userStatus', here is the child:
Component({
selector: 'profile',
template: require('app/components/profile/profile.html'),
providers: [],
directives: [],
pipes: []
})
export class Profile implements OnInit {
constructor() {
}
ngOnInit() {
// I want to have access with the parent App Component, 'userStatus' propety here... I only want to read this property
}
}
Now in Angular 1.x this would be easy as I could reference $parent in my child controller or (ANTI PATTERN ALERT!!!) I could be so foolish to put this data in my $rootScope.
What would be the best way to access the parent in Angular 2?
There are different way:
global service
service shared by parent and injected to the child
providers or viewProviders in the parent instead of boostrap(...) and only available to children of parent.parent injected to the child and accessed directly by the child
export class Profile implements OnInit {
constructor(@Host() parent: App) {
parent.userStatus ...
}
export class Profile implements OnInit {
@Input() userStatus:UserStatus;
...
}
<profile [userStatus]="userStatus">
I had the same problem but I solved it differently. I don't know if it's a good way of doing it, but it works great for what I need.
I used @Inject on the constructor of the child component, like this:
import { Component, OnInit, Inject } from '@angular/core';
import { ParentComponent } from '../views/parent/parent.component';
export class ChildComponent{
constructor(@Inject(ParentComponent) private parent: ParentComponent){
}
someMethod(){
this.parent.aPublicProperty = 2;
}
}
This worked for me, you only need to declare the method or property you want to call as public.
In my case, the AppComponent handles the routing, and I'm using badges in the menu items to alert the user that new unread messages are available. So everytime a user reads a message, I want that counter to refresh, so I call the refresh method so that the number at the menu nav gets updated with the new value. This is probably not the best way but I like it for its simplicity.
I made a generic component where I need a reference to the parent using it. Here's what I came up with:
In my component I made an @Input :
@Input()
parent: any;
Then In the parent using this component:
<app-super-component [parent]="this"> </app-super-component>
In the super component I can use any public thing coming from the parent:
Attributes:
parent.anyAttribute
Functions :
parent[myFunction](anyParameter)
and of course private stuff won't be accessible.