Temporarily suspend data-binding in AngularJS to update model without firing $scope.$watch -
update 4/19/2012 12pm pst: gotta eat crow on one. problem not angular's databinding math errors in how calculating dates. wasn't taking account minutes , seconds in time calculations. subtracting timestamps each other expecting hours come out nicely.
i've gotten myself trouble angularjs databinding in different inputs need reciprocally bound each other.
the form needs contain following:
- a start date (this given, not input)
- an input add hours on start date
- two inputs result: 1) date picker , 2) hour picker
if change of inputs, should update others. so, following desirable results:
(with original date-time april 19th @ 10pm). user enters '1 hour'. result date becomes april 19th , result time becomes 11pm.
building on above example, user changes date input april 20th. 'hours' become 25 hours.
i've placed set watchers, using $scope.$watch on each of these variables:
$scope.$watch('hours', function (newhours, oldhours, scope) { if (newhours) { var newenddate = new date(scope.origdate), offhours = newenddate.gethours(); newenddate.sethours(offhours + newhours); scope.enddate = newenddate; scope.endhours = newenddate.gethours(); } }); $scope.$watch('endhours', function (newendhours, oldendhours, scope) { if (newendhours) { var newenddate = new date(scope.enddate); newenddate.sethours(newendhours); scope.enddate = newenddate; } }); $scope.$watch('enddate', function (newenddate, oldenddate, scope) { if (newenddate) { scope.hours = (newenddate - scope.origdate) / (1000 * 60 * 60); } }); each of these works fine on own, in tandem cause big fat mess. understanding of angular, seems they're creating 'feedback loop.' edit: wit, model 'a' updated , trigger change on model 'b'. model 'b' trigger change on model 'a'.
now, possible temporarily suspend data-binding can just update models without firing watchers? in other contexts i've worked in (uitableview on cocao comes mind), 1 can ask "stop updates," make changes model, , "resume updates."
is there in angularjs? if not, not getting here, , how set project achieve desired functionality?
here's plunkr of example.
Comments
Post a Comment