当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - angularjs - set certain inputs with data to $pristine or untouched correctly

问题描述:

After researching i am still stuck on my predicament. I am using the latest version of foundation for apps with angular 'pre-embedded'.

I have a form which is used for both adding and editing data - lets say its for a job and it tracks employees days on the job. on load of the view the form is completely empty and contains two sections - the job details and the employees details.

first the user would have to enter and save the details in the job section (in order to create the id for the job so the employees can be linked to it) for the employees section to be activated. This works fine and is enabled by the job id being set to > 0 (-1 being default on load);

This all works fine so far, but i have a function in my controller to allow loading in an existing job record. when the user blurs on one of three fields the controller checks the data object for jobs matching these unique fields and loads (angular.copy) in the data in the scopes 'job' model. When this is loaded all form inputs are set and marked as dirty.

obviously my required behavior is that if the job details are loaded in by the system the form is not marked as dirty so as to allow me to know if the job details have been edited at any point after. the same is required of a new job that has been saved and given an id.

as i understand it the setPristine() function actually resets the form and model, similar to form[0].reset() but I may be wrong. I also suspect untouched may be a way to go?

also i should mention that the contains all sections to this view including the add employee inputs in the employee section.

I understand I can simply remove the ng-dirty class manually but have read this then does not update the angular way and will not identify further edits.

Is there a way i can functionally remove the ng-dirty from all child inputs of an element and not all in the form and without removing the loaded data too?

note: all inputs by default are linked (via ng-model) to their relevant models and data is loaded in the controller to the model.

网友答案:

It seems I've misunderstood pristine from the other posts, after further tests it is only the field that the unique data was entered into that is changed to dirty. I simply need to call setPristine on this after loading the existing record and then maybe loop through all elements within this section to test them. $setPristine does not clear the input as i read previously.

分享给朋友:
您可能感兴趣的文章:
随机阅读: