Passing an object from the parent controller to an isolated child directive

I have an object that I want to see inside the directive.
The directive has an isolated scope, and the object comes from the parent controller.

DOM:
<div hello-directive obj-to-track="{{myObj}}"></div>

JS Directive:

 scope:{ objToTrack:'@' }, link:function(scope,element,attrs){ scope.$watch(function(newValue){ //Inside here, newValue is a JSON string //So is scope.objToTrack }); } 

In any case, to get the real object from the parent controller other than JSON.parse()
Thanks.

+6
source share
1 answer

Just use the "=" binding:

 scope:{ objToTrack: "=" } 

See as usual:

 scope.$watch("objToTrack", function(newval, oldval) { ... }); 

Use it as:

 <div hello-directive obj-to-track="myObj"></div> 
+8
source

All Articles