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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With