3

I'm developing an Angular App but I want to catch onclick event without ng-click, I want to use something like that

$scope.onClick(...)

6
  • Why? what purpose would that serve? Commented Aug 10, 2016 at 20:25
  • @KevinB I want to separate all logic and use event handlers for it. Commented Aug 10, 2016 at 20:43
  • 1
    but... that's what ng-click does... binds event handlers... you can still have the handler defined in the controller. Commented Aug 10, 2016 at 20:45
  • You could use a directive, but then... you'd have ot put the directive on the html, just like ng-click, so it's no better. Commented Aug 10, 2016 at 20:46
  • 1
    If you're trying to avoid ng-click, you're probably looking for a different framework. Backbone.js does what you're describing pretty much. Commented Aug 10, 2016 at 20:52

3 Answers 3

2

AngularJs use directive to operate dom. you can add a directive like this.

AngularJs

YourApp.directive('testClick', function () {
    return {
        restrict: 'A',
        link: function (scope, element) {
            element.onclick = function() {
                //do some thing.
            }
        }
    }
})

html

<button test-click>Test Click</button>
Sign up to request clarification or add additional context in comments.

Comments

1

Depends on what you're trying to click. If we're talking about normal DOM,you could use the regular, non-angular way of doing this.

I.e.

Assuming for an element like

<div id="elementID" onclick = "clicked">Element</div>

Javascript:

function clicked(){
     console.log("I was triggered!");
}
var element = document.getElementById('elementID');
element.onclick = function(){
     console.log("I was also triggererd!"); 
}

Or even use JQuery if you want:

$('#elementID').bind('click', function () {
    console.log("I would also be triggered!");
});

Hope this helped!

4 Comments

Thanks, but I want to use with Angular.js
So why exactly do you want to use onclick() without ng-click()?
Because I have all my logic on Angular Controller and services, but I want to declare on my Controller something like $scope.Element.OnClick(...)
But what difference does it make to have $scope.clickfunc = function(){...} with 'ng-click = "clickfunc()"' instead of what you want? Theres almost no reason to use onclick as angular provides you with the ng-click functionality.
0

The only way it worked for me was:

JS:

    .directive('testClick', function () {
        return {
            restrict: 'A',
            link: function (scope, element) {
                element.on('click', function() {
                    console.log('Clicked');
                })
            }
        }
    })

HTML:

<button test-click> Here </button>

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.