Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
By-passing $dirty check
Message
 
 
À
01/04/2015 13:22:18
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01609780
Message ID:
01617654
Vues:
13
For some reason focus doesn't seem to work at all - I can not get the break point to execute when I open Developer's tools in debugger.

Not sure what is wrong now, why only one event seems to fire.
 element[0].focus(function () {
                    if (form)
                        formDirtyState = form.$dirty; // save form's dirty state
                 });
                
                element.bind('blur', function () {
                    if (currentControl) {
                        currentControl.$dirty = false; // Remove dirty state but keep the value
                        if (!formDirtyState && form)
                            form.$setPristine();

                        //          scope.$apply();
                    }
                });
>>>Think that may be overkill - you'll be setting form to pristine even if other inputs are dirty.
>>>
>>>>I think I got it to work, but I am continuing tests now. Here is my current code for the directive, let me know if you see problems:
>>>>
>>
>>Do you think saving current form's state in focus event is not going to work? Do you see my variable for saving current form's state?
>Quick reply so I may have missed something but if the form is NOT dirty there's no point in setting it to pristine. And, as before, if it IS dirty then that may be because an input other than the one you are interested in is dirty......
>
>>
>>>>
>>>>(function () {
>>>>    'use strict';
>>>>
>>>>    var app = angular.module('sysMgrApp');
>>>>
>>>>    app.directive('noDirtyCheck', [function () {
>>>>        // Interacting with input elements having this directive won't cause the
>>>>        // form to be marked dirty.
>>>>        // http://stackoverflow.com/questions/17089090/prevent-input-from-setting-form-dirty-angularjs
>>>>        return {
>>>>            restrict: 'A',           
>>>>            require: ['^form', '^ngModel'],
>>>>
>>>>            link: function (scope, element, attrs, controllers) {
>>>>                var form = controllers[0];
>>>>                
>>>>                var currentControl = controllers[1];
>>>>
>>>>                var formDirtyState = false;
>>>>
>>>>                element[0].focus(function () {
>>>>                    if (form)
>>>>                        formDirtyState = form.$dirty; // save form's dirty state
>>>>                 });
>>>>                
>>>>                element.bind('blur', function () {
>>>>                    if (currentControl) {
>>>>                        currentControl.$dirty = false; // Remove dirty state but keep the value
>>>>                        if (!formDirtyState && form)
>>>>                            form.$setPristine();
>>>>
>>>>              //          scope.$apply();
>>>>                    }
>>>>                })
>>>>            }
>>>>        };
>>>>    }]);
>>>>})();
>>>>
>>>>
>>>>>>>Bear in mind that simply setting an inputs $dirty to false (even when there are no other dirty controls) will not cause the form $dirty to be re-evaluated. You may need something like this in your directive after $setPristine():
var dirty = false;
>>>>>>>angular.forEach(scope.theForm, function (value, key) {
>>>>>>>    if (key[0] != '$') {
>>>>>>>        if (value.$dirty) {
>>>>>>>                dirty = true;
>>>>>>>        }
>>>>>>>    }
>>>>>>>});
>>>>>>>if (!dirty) {
>>>>>>>scope.theForm.$setPristine();
>>>>>>>}
This will reset the form if all inputs are pristine.....
>>>>>>>
>>>>>>
>>>>>>For this code to work do I need to add
>>>>>>
>>>>>>require: ['^form'],
>>>>>>
>>>>>>and use scope.form syntax where you used scope.theForm
>>>>>>
>>>>>>?
>>>>>
>>>>>This, although a bit messy, seems to work:
                      var dirty = false;
>>>>>                       var theForm = null;
>>>>>                       angular.forEach(scope.theForm, function (value, key) {
>>>>>                           if (key == '$$parentForm') {
>>>>>                               theForm = value;
>>>>>                           }
>>>>>                           if (key[0] != '$') {
>>>>>                               if (value.$dirty) {
>>>>>                                   dirty = true;
>>>>>                               }
>>>>>                           }
>>>>>                       });
>>>>>                       if (!dirty) {
>>>>>                           theForm.$setPristine();
>>>>>                       }
(Uses the control to reach up to the parent form)
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform