I don’t understand how to watch multiple attributes at the same time in the link function, so I create an object with all the parameters and I watch for it. But I noticed that the attribute in the link function is a string and not an object so I’m using angular.fromJson(val).
All the example I found just use one parameter
Could you explain how to watch multiple attributes?
Thanks
EDIT:
I cannot use attrs parameter because I need to bind the attributes — i.e., they require interpolation. For example
<ul class="thumbnails">
<li class="span3" ng-repeat="image in currentSizeInfo.images" >
<upload-file info = "{{getInfo($index)}}" foo="foo$index" ></upload-file>
</li>
</ul>
I think that I have to use $watch
link:function (scope, element, attrs ) {
scope.$watch('info', function (val) {
// if info is and foo is .... do all the stuff
})
}
I’m not sure I fully understand your question, so please correct me if I misunderstand. Are just want to pull values from multiple attributes on your directive? So say you have a HTML like this:
And you want to get the values of those different attributes? In the link function, you just use the attrs parameter. For example:
You can also use the scope property on the directive to automatically bind attributes to your scope. See their documentation on directives. So, something like this:
And then those properties end up in your scope automatically. However, as I found out, those values aren’t always in the scope when you’d expect. So you can use the
$watchfunction to do what you need with them. Something like:If you need to use them all together at the same time, you could use a function for the first parameter of the
$watchthat returns a string that would be different once they are all there and then put your logic in the function that is the 2nd parameter. So something like this:If you’re wanting to bind objects into your scope, you can use ‘=’ instead of ‘@’. Another option is ‘&’ which evaluates a function in the parent scope. This is all explained in the directive documentation linked above.