find updated field in object - scope.$watch - angularjs -
if do:
scope.$watch('obj', function(newobj, oldobj) { //... }, true);
how find key-value pair in object changed?
.
only understand try do:
i have object of form:
scope.actions = { action1: false, action2: false }
when boolean changes, want assign function calls it. like, do-action - undo-action.
so watch following way:
scope.$watch('actions', function(newobj, oldobj) { /*pseudo code start*/ if (action1 changed && action1 true) { do-func1(); } if (action1 changed && action1 false) { undo-func1(); } ... /*pseudo code end*/ }, true);
my problem here is, if check values boolean, functions called. point here is, how find changed key-value pair in object?
scope.$watch('actions', function(newactions, oldactions) { for(var in newactions) { if(newactions.hasownproperty(i)) { var newaction = newactions[i]; var oldaction = oldactions[i]; if(newaction !== oldaction) { if(newaction === true) { doactions[i](); // do-func-i(); } else if (newaction === false) { undoactions[i](); // undo-func-i(); } } } } }, true);
doactions
here conventional map of actions
doactions = { action1 : function() { console.log('action1'); }, action2 : function() { console.log('action2'); } }
doactions['action1']
reference first function
you may have array well, you'll need introduce index fetch proper function doactions[0]
Comments
Post a Comment