Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing object from parent controller to isolated scope child directive

Tags:

angularjs

I have an object that I want watched inside a directive.
The directive has isolated scope and the object comes from a parent controller.

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

Directive JS:

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

Is there anyway to get an actual object from the parent controller besides JSON.parse()
Thanks.

like image 513
Francisc Avatar asked Dec 10 '13 21:12

Francisc


1 Answers

Just use the "=" binding:

scope:{
    objToTrack: "="
}

Watch it as usual:

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

Use it as:

<div hello-directive obj-to-track="myObj"></div>
like image 76
Nikos Paraskevopoulos Avatar answered Nov 20 '22 12:11

Nikos Paraskevopoulos