0

I would like to do the following:

<my-directive my-object="{Code:'test'}"/>

Then convert the string to an object in the controller. Currently using JSON.parse

I know I can do this:

<my-directive my-object="{&quot;Code&quot;:&quot;test&quot;}"/>

But seems messy and hard to read

Any way to do this?

Jester
  • 3,069
  • 5
  • 30
  • 44
  • 3
    Why not declaring that in the controller `$scope.obj = {Code: 'test'};` and pass `my-object="obj"` ? – Alon Eitan Jun 05 '16 at 18:35
  • cos the goal is to try to do not have each of this objects in the scope. So that a person that does not know much about angular but has a certain understanding of html can produce that in a easy manner – Jester Jun 05 '16 at 18:42
  • And BTW, I think that if you declare the property in the directive's scope as `myObj = "="` then it should work just fine – Alon Eitan Jun 05 '16 at 18:42
  • it is defined as myObj= but still comes as string – Jester Jun 05 '16 at 18:43
  • Take a look at this example https://plnkr.co/edit/gAQOEBdTz5UQG6IOpzN0?p=preview I cahgned the plunker from the [tutorial](https://docs.angularjs.org/guide/directive) so it will take the data directly from the view and it's working – Alon Eitan Jun 05 '16 at 18:51

1 Answers1

1

It's not pretty, but it is valid to simply do:

<my-directive my-object='{"Code":"test"}'/>
Amit
  • 45,440
  • 9
  • 78
  • 110