0

I'm using typescirpt in Angular.

I've created a directive that is just a div containing text and a close button with a class name 'block-close'

I need to add an click function to the close button.

How do I add a click event to button that is inside the directive.

I've tried things like

    angular.element('.block-close').on('click', function(){
        alert('here');
    });

    angular.element(document).find('.block-close').on('click', function(){
        alert('here');
    });

    (()=>{

        class MyDirective implements ng.IDirective{

            public restrict = 'E';
            public scope = {};
            public controller = 'MyController';
            public controllerAs = 'myCtrl';
            public bindToController = true;
            public templateUrl = "mysite.html";

            constructor(){

            }

            link = (scope, element, attrs) => {
                angular.element('.block-close').on('click', function(){
                    alert('here');
                });
            };

        }

        angular.module('myMod').directive('myDirective', ()=> new MyDirective());

    })();

1 Answer 1

0

I am just assuming that your "mysite.html" has code like this

<div>
<input type="button" value="close" class="block-close"/>
</div>

And you want to handle click event for added button has class "block-close".

Actually inside link function you can access scope of that directive so just add one function shown below,

link = (scope, element, attrs) => {
                scope.closeClicked = () =>{
                           alert("clicked..");
                     }
            };

and update your "mysite.html" like below

<div>
    <input type="button" value="close" ng-click="closeClicked()" class="block-close"/>
</div>
Sign up to request clarification or add additional context in comments.

Comments

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.