1

I have defined a custom click directive as below:

(function() {
    
    'use strict';
    angular.module('myApp.core')
        .directive('customClick', customClick);

        customClick.$inject = ['$rootScope'];

        function customClick() {
            return {
                restrict: 'A',
                /*scope: {
                    customClick: '&'  
                },*/
                link: function(scope, element, attrs){
                   $(element).on('click', function(e) {
                        scope.$apply(function() {
                            console.log("Hello..customClick..");
                            scope.customClick();
                        });
                    });
                }
          };
        }
})();

And I get the following error on this;

Error logged by WDPR Angular Error handler service {xx.."stacktrace":"TypeError: a.customClick is not a function","cause":"unknown cause"}(anonymous function)bowerComponents.js:5745

How can I resolve this? If I add scope with '&' I get demanding isolated scope. Hence how to resolve it?

If I remove - scope.customClick();, it does not show anything on second html for custom-click, it has impact on only 1 html, and its controller. I want to use it in multiple controller + html.

2 Answers 2

1

customClick is a function on the directive itself. It is not a function on the scope. That's why the error has occurred.

link is used to manipulate dom/add event handlers on elements, which you have rightly done with element.bind('click', function() {

Whenever click occurs, the function binded to click automatically gets invoked. There is no need of watch and the invoking statement.

Your link can just be

link: function(scope, element){
        element.bind('click', function() {
                console.log("Hello..customClick..");
        });
    }

As you have used camel case in naming the directive, exercise caution in its usage in template.

You can use it as <p custom-click>hi</p>

Sign up to request clarification or add additional context in comments.

6 Comments

should i be adding this - scope: { customClick: '&' } or not?
You can add it. It is prominent when you are binding the value from your parent template to this directive. & is for one way binding. = is for two way binding. Here, it doesn't make any difference as we are not assigning any value to the directive.
if i add it, I get - multiple directives [customClick, customClick] asking for new/isolated scope on:xxx template. So i removed it. But, problem now is - I see in console, "Hello custom click" twice for 1 single click!
I am afraid I am not getting any such error. Fiddle here: jsfiddle.net/rn175w2y Please show any fiddle demo for further help. thanks
just one question - is the error because i am trying to use it in multiple controller?
|
0

I would recommend you to avoid using jQuery in angular apps. Try following

angular.module('newEngagementUI.core')
        .directive('customClick', customClick);

    customClick.$inject = ['$rootScope'];

    function customClick() {
        return {
            restrict: 'A',
            scope: {
                customClick: '&'  
            },
            link: function(scope, element, attrs){
               element.bind('click', function () {
                   scope.customClick();
               })
            }
      };
    }

In your template:

<div custom-click="clickFunction"></div>

And your template controller should be like:

angular.module('myApp', []).controller(['$scope', function ($scope) {

    $scope.clickFunction = function () {
        alert('function passed');
    }

}])

Working fiddle here: https://jsfiddle.net/xSaber/sbqavcnb/1/

3 Comments

with this, i get the following error: multiple directives [customClick, customClick] asking for new/isolated scope on:xxx template
@Smitha I have created it in a separate fiddle: jsfiddle.net/xSaber/sbqavcnb/1
Thanks for the fiddle. But we should be destroying this timeout right? how do we do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.