AngularJS - knowing when the data has been bound to DOM -
i'd determine if length text exceeds width of container in order that, need know when data has been bound dom.
is there event in angularjs tells me page has been bound, or using settimeout way sidestep that?
well, depends;
one-time evaluation
if data not changed after linked, it's matter of using postlink() function within directive. function tied link property of directive default, can evaluate data on spot:
link: function(scope, element, attrs) { // element , data linked, let's mess around dom! } dynamic data
if data going change after linking phase (after dom compiled , scope bound it), it's bit more convoluted.
there's no way of knowing when digest cycle has ended (or when data has finished updating), each evaluation may change data being evaluated , trigger cycle.
the best can in such cases arbitrarily decide when data has finished changing - can $watch changes on bounded data, , decide e.g. if text not soft-equaling former text value, perform action in dom.
link: function(scope, element, attrs) { scope.$watch('counter', function(newval, oldval, curscope) { if (newval != oldval) { // data re-evaluated, let's mess around dom! } }); } see demo dynamic data evaluation.
post scriptum
note talking detecting when data bound dom, , not whether element attached dom. shouldn't matter in cases, it's worth noting.
Comments
Post a Comment