facultymatt / angular-unsavedChanges

angular-unsavedChanges
175 stars 81 forks source link

How to apply the directive AFTER data is loaded through HTTP? #46

Open samlevin opened 9 years ago

samlevin commented 9 years ago

Any time I add values to a form (that are loaded through HTTP), this library always thinks my form is dirty. Any easy workaround? I want the dirty check to start taking place only after a form is initialized.

Thanks a ton for your quick guidance.

cstephe commented 9 years ago

Can you post how you are loading data. Form dirty normally doesn't get set unless you are triggering user events.

samlevin commented 9 years ago

Basically making a call to an HTTP service, which returns an object (in a predetermined format), and then I bind that object to scope in a controller. The UI template uses this controller, and I'm using 'ng-model' (on the $scope object properties) to display the object properties in the UI

EDIT: After I load the controller/template with the form, the log states 'all forms are clean'. However, when I try to click another object (which loads a new form), I then get a log message stating 'a form is dirty', even after I've made 0 clicks on the UI (aside from opening up a new form).

samlevin commented 9 years ago

It seems like $pristine() is called after the directive loads, but before the HTTP call has finished.

awerlang commented 9 years ago

Make sure your form is in $pristine state while it is loading data. Perhaps a specific control your are using is setting your form as $dirty.

It may help to pinpoint the exact source of error if you put a breakpoint on $setDirty() to see who is calling that.