Does calling a controller method in a directive work with scope, but not with attrs?

I want to call the controller method after the directive made a change in the DOM, but it did not work, so I send this method in the directive, which is called inside scope.$watch

.directive('checkThis', function($timeout) {
    return {
        restrict: 'E',
        scope: {
            myData: '=',
            dirty: '=',
            what: '@',
            callback: '&'
        },
        link: function(scope, element, attrs) {
            scope.$watch('myData', function(newValue, oldValue) {
                if (newValue !== oldValue) {
                    if (newValue) {
                        scope.dirty++;
                    } else { 
                        scope.dirty--;
                    }
                    check();
                }
            });

            var check = function () {
                if (angular.isDefined(scope.callback)) {
                    $timeout(function() {
                        console.log('being called');
                        scope.callback();
                    }, 0, false);
                }
            };
        }
    };
});

and using this directive in HTML as shown below

<check-this my-data="vm.encryption" dirty="vm.dirty" what="column" callback="vm.checkAll()"></check-this>

now, when I use attrs.callback, it is not executed, but it scope.callback()is executed, even when I check attrs.callback, it displays the method in the console.

why is this behavior so?

even i tried scope.$eval(attrs.callback)and

 scope.$apply(function() { 
    scope.$eval(attrs.callback); 
 });

but he is not called. I take the link from in this article

+4
source share
1 answer
Directive

Angular attrs - , attrs.callback - 'vm.checkAll()' . $eval , , .

scope.callback(), , ?

- attrs.callback, scope.$parent.$eval(attrs.callback); eval('scope.$parent.' + attrs.callback), vm.checkAll() .

scope.$eval(attrs.callback) , .

0

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


All Articles