angular JS directive - controller vs link vs compile
compile: used when we need to modify directive template, like add new expression, append another directive inside this directive
controller: used when we need to share/reuse $scope data
link: it is a function which used when we need to attach event handler or to manipulate DOM.
so in short, compile is angular is travelling through each directive and make them to DOM.
You do not have $scope variable ready.
After that, there is link: where you can access DOM elements and also use $scope of current directive
link has pre-link and post link function, if you dont specify, link function will be default to post link.
Then post link is called in reverse order so that when its at parent directive, the children are all avialble (called).
Pre-link is called in same order as compile because you want to do something before post you have access to $scope as well,
controller is same as MVC and same order as pre-link
https://www.youtube.com/watch?v=faXWW6WjTTE
https://www.jvandemo.com/the-nitty-gritty-of-compile-and-link-functions-inside-angularjs-directives/
https://stackoverflow.com/questions/20018507/angular-js-what-is-the-need-of-the-directive-s-link-function-when-we-already-ha
https://docs.servicenow.com/en-US/bundle/vancouver-application-development/page/build/servicenow-cli/concept/servicenow-cli.html
No comments:
Post a Comment