0
votes

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?

1
If you don't pass it, you can't. You could share it through a service though, but that's another story - baao
How do you pass it when using router-outlet? - bruh
@Vikas you downvoted because you can't read. Sebastian Hildebrandt recommended using a service to pass data. Thats why I upvoted his comment, because he made a valid suggestion and not a useless link like you did. I clearly stated that I'm looking for a way to pass data without using templates. Whats not clear about that? If you don't know the answer, don't answer. I see you get your upvotes by posting useless links and hope to get celebrated by other passive aggressive people instead of actually answering things - bruh

1 Answers

4
votes

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

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