How do I ignore the initial load when watching model changes in AngularJS? -
i have web page serves editor single entity, sits deep graph in $scope.fieldcontainer property. after response rest api (via $resource), add watch 'fieldcontainer'. using watch detect if page/entity "dirty". right i'm making save button bounce want make save button invisible until user dirties model.
what getting single trigger of watch, think happening because .fieldcontainer = ... assignment takes place after create watch. thinking of using "dirtycount" property absorb initial false alarm feels hacky ... , figured there has "angular idiomatic" way deal - i'm not 1 using watch detect dirty model.
here's code set watch:
$scope.fieldcontainer = message.get({id: $scope.entityid }, function(message,headers) { $scope.$watch('fieldcontainer', function() { console.log("model dirty."); if ($scope.visibility.savebutton) { $('#savemessagebuttonrow').effect("bounce", { times:5, direction: 'right' }, 300); } }, true); });
i keep thinking there's got cleaner way guarding "ui dirtying" code "if (dirtycount >0)"...
set flag before initial load,
var initializing = true
and when first $watch fires,
$scope.$watch('fieldcontainer', function() { if (initializing) { $timeout(function() { initializing = false; }); } else { // whatever going } });
the flag tear down @ end of current digest cycle, next change won't blocked.
Comments
Post a Comment