I already know how to check if certain input elements on my form are dirty or not, but I was wondering if there was a quick way to just iterate over just the $dirty ones only? I know Angular sets a ng-dirty class on the elements, and I can figure out how to do this in jQuery, but I can't figure out how to do it in AngularJS context.
问问题
2235 次
1 回答
4
不久前,我写了一个过滤器(在社区的帮助下)来做这件事,它对我很有帮助。
/* App Module */
angular.module("dirtyFilter", []).
filter("returnDirtyItems", function () {
return function (modelToFilter, form, treatAsDirty, removeTheseCharacters) {
//removes pristine items
//note: treatAsDirty must be an array containing the names of items that should not be removed
for (var key in modelToFilter) {
//delete the item if:
// * it exists on the form and is pristine, or...
// * does not exist on the form
try{
//console.log("checking " + key + " for pristine and found it is " + form[key].$pristine);
}
catch(err){
//console.log("key " + key + " did not have an element in the form");
}
if (removeTheseCharacters != undefined && removeTheseCharacters.length > 0) {
for (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 the index of " + key + " throws an error of " + err + " so we skipped it");
}
}
}
if ((form[key] && form[key].$pristine) || !form[key]) {
//delete the item if the treatAsDirty argument is not present
//console.log("Checking to see if " + key + " is to be treated as always dirty");
if(treatAsDirty){
//console.log("There is an array present for treatAsDirty");
//delete the item if it is not in the treatAsDirty array
if(treatAsDirty.indexOf(key) == -1){
//console.log("The item " + key + " was not found in the always dirty array and has been deleted");
//remove the pristine item from the parent object
delete modelToFilter[key];
} else {
//console.log("The item " + key + " was found in the always dirty array and has been kept");
}
} else {
//console.log("There is no array present for dirty items, so " + key + " will be removed");
//remove the pristine item from the parent object
delete modelToFilter[key];
}
}
}
return modelToFilter;
}
});
于 2013-07-25T17:31:23.957 回答