Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Pass object to directive

Tags:

angular

I'm looking for a way to pass an object into a directive, but it turns up as a string.

Given this template:

<div my-directive="{ name: 'Roger', orders: ['apple', 'pie'] }"></div>

How can I get an object back in the directive?

@Input('my-directive') initialData: any; //initial data is a string

I assume maybe something with JSON.parse, but not sure how?

like image 796
Thibs Avatar asked May 08 '26 04:05

Thibs


1 Answers

You have to use binding [] syntax

<div [my-directive]="{ name: 'Roger', orders: ['apple', 'pie'] }"></div>

otherwise it is treated a string, and you want object here.

like image 189
Antoniossss Avatar answered May 10 '26 01:05

Antoniossss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!