bruh
bruh

Reputation: 2305

Angular access parent variable from child component

How do I get a variable from the parent, from a child component?

app.component

secret: string = '123'

child.component

@Input() secret;

console.log(this.secret);
=> undefined

Every example only shows template binding, for example [secret]="secret"

How do I just access the parent component's data from the child component?

Upvotes: 3

Views: 5738

Answers (1)

Sebastian Hildebrandt
Sebastian Hildebrandt

Reputation: 2791

Try it with property binding in the HTML of your parent (where you are using the child component) like so:

<child [secret]="secret"><child>

Upvotes: 5

Related Questions