AngularJS iterate over $dirty elements -
i know how check if input elements on form dirty or not, wondering if there quick way iterate on $dirty ones only? know angular sets ng-dirty class on elements, , can figure out how in jquery, can't figure out how in angularjs context.
i wrote filter (with community) while , has served me pretty well.
/* app module */ angular.module("dirtyfilter", []). filter("returndirtyitems", function () { return function (modeltofilter, form, treatasdirty, removethesecharacters) { //removes pristine items //note: treatasdirty must array containing names of items should not removed (var key in modeltofilter) { //delete item if: // * exists on form , pristine, or... // * not exist on form try{ //console.log("checking " + key + " pristine , found " + form[key].$pristine); } catch(err){ //console.log("key " + key + " did not have element in form"); } if (removethesecharacters != undefined && removethesecharacters.length > 0) { (var ca = 0, len = removethesecharacters.length; ca < len; ca++ ) { try{ //console.log("index of " + key + " is: " + modeltofilter[key].indexof(removethesecharacters[ca])); if (modeltofilter[key].indexof(removethesecharacters[ca]) >= 0) { modeltofilter[key] = modeltofilter[key].replace(removethesecharacters[ca], "", "g"); } } catch(err){ //console.log("getting index of " + key + " throws error of " + err + " skipped it"); } } } if ((form[key] && form[key].$pristine) || !form[key]) { //delete item if treatasdirty argument not present //console.log("checking see if " + key + " treated dirty"); if(treatasdirty){ //console.log("there array present treatasdirty"); //delete item if not in treatasdirty array if(treatasdirty.indexof(key) == -1){ //console.log("the item " + key + " not found in dirty array , has been deleted"); //remove pristine item parent object delete modeltofilter[key]; } else { //console.log("the item " + key + " found in dirty array , has been kept"); } } else { //console.log("there no array present dirty items, " + key + " removed"); //remove pristine item parent object delete modeltofilter[key]; } } } return modeltofilter; } });
you can see in action @ http://jsfiddle.net/mbielski/sdn2h/1/
Comments
Post a Comment