genkio / blog

Stay hungry stay foolish
https://slashbit.github.io/blog/
0 stars 1 forks source link

Difference between directive controller and link function #105

Open genkio opened 8 years ago

genkio commented 8 years ago

“Am I just doing template and scope things?” – goes into controller “Am I adding some coolbeans jquery library?” – goes in link

source

(1) After setting up the view[V](i.e. template) with link, $scope is our [M] and $controller is our [C] in M V C

(2) Take advantage of the 2-way binding with $scope by setting up watches.

(3) $scope watches are expected to be added in the controller since this is what is watching the template during run-time.

(4) Finally, controller is also used to be able to communicate among related directives. (Like myTabs example in https://docs.angularjs.org/guide/directive)

(5) It's true that we could have done all of this in the link function as well, but it's about separation of concerns.

source