Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $watch expressions in directive link vs controller function?

I've seen this done both ways; What is the difference between creating a scope.$watch in a directive controller vs the linking function?

like image 514
ThinkingInBits Avatar asked Oct 15 '13 12:10

ThinkingInBits


2 Answers

There is no real difference between the two. At the matter of fact, the $watch expression will be accessible from both the controller and the directive regardless to where it was initially defined, as they both share the same $scope. That being said, a $watch in defined in the directive has a completely different purpose than a one defined in the controller.

like image 75
yohairosen Avatar answered Oct 21 '22 14:10

yohairosen


There is no difference as far as I can tell. The scope passed in the linking is: "Scope - The scope to be used by the directive for registering watches."

So if you don't explicitly declare a controller or scope for the directive you still have a scope (taken from the context in which the directive is being compiled) to add watches to in the link function returned by the compile.

like image 23
Erstad.Stephen Avatar answered Oct 21 '22 13:10

Erstad.Stephen