Unable to view form of $ dirty (or $ primitive) value in Angular 1.x

I have a script in an Angular 1.x project where I need to look at the controller form inside the directive in order to perform a fake $ form check. Once the form on the page is dirty, I need to set a flag in the injected service.

Here is the general directory code:

            var directiveObject = {
            restrict: 'A',
            require: '^form',
            link: linkerFn,
            scope: {
                ngConfirm: '&unsavedCallback'
            }
        };

        return directiveObject;

        function linkerFn(scope, element, attrs, formCtrl) {
          ...

            scope.$watch('formCtrl.$dirty', function(oldVal, newVal) {
              console.log('form property is being watched');
          }, true);               

          ...
        }

The above only goes into the clock during initialization, so I tried other approaches with the same result:

view the scope of $ parent [formName]. $ dirty (in this case, I pass formName to attrs and set it to local var formName = attrs.formName)

view element.controller () [formName] (same result as above)

SO, , . , , - ( ) .

. .

+4
1

, , $dirty . :

.directive('formWatcher', function() {
    restrict: 'A',
    scope: {
        ngConfirm: '&unsavedCallback', // <-- not sure what you're doing with this
        isDirty: '='
    },
    link: function(scope, element, attrs) {
        scope.watch('isDirty', function(newValue, oldValue) {
            console.log('was: ', oldValue);
            console.log('is: ', newValue);
        });
    }
})

:

<form name="theForm" form-watcher is-dirty="theForm.$dirty">
    [...]
</form>
+3

Source: https://habr.com/ru/post/1648708/


All Articles