angularjs - When does Angulars link function run? -
from understand runs once before page rendered. there ever case runs after page has been rendered?
i tried testing few things plnkr:
angular .module('app', []) .directive('test', function($http) { return { restrict: 'e', template: '<input />', link: function(scope, el, attrs) { var input = angular.element(el[0].children[0]); input.on('change', function() { console.log('change event'); scope.$apply(function() { console.log('digest cycle'); }); }); input.on('keyup', function() { console.log('keyup event'); var root = 'http://jsonplaceholder.typicode.com'; $http.get(root+'/users') .success(function() { console.log('http request successful'); }) .error(function() { console.log('http request error'); }); }); console.log('link function run'); } }; }); - does typing in input field cause link function run?
- do event listeners cause link function run?
- do http requests (made
$http?) cause link function run? - do digest cycles cause link function run?
the answer of these questions seem "no".
the link function runs when instance of directive compiled, in case, when <test></test> element created. can when angular's bootstrapping compiles page, when comes being ng-if, when ng-repeat makes it, when it's made $compile, etc.
link never fire twice same instance of directive. notably, fires right after template has been compiled in directive's lifecycle.
Comments
Post a Comment