0

Most of ng-click function defined in html, like this:

<span type="button" id='test' ng-click="dosth()"></span>

and define the function in controller, many like this:

$scope.dosth = function () {};

, but if we didn't add a function in html, we can define a jquery click function just in js file, like this:

<span type="button" id='test'></span>

$('#test').click(function() { do sth ... });

So can we define a angular ng-click functioin only in a js file?

Qi Tong
  • 79
  • 2
  • 12
  • see this tutorial https://docs.angularjs.org/tutorial/step_10 – Vadim Nov 24 '14 at 15:47
  • and see this answer http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background/15012542#15012542 – Joao Leal Nov 24 '14 at 15:50
  • @Vadim Sorry, I can't vist the site. Can you explain or give a sample? – Qi Tong Nov 24 '14 at 15:57
  • @CuriosityTong, that's the angularjs documentation site, if you can't visit it, you're going to have a very hard time learning angular (all the documentation is there). To answer your question simply, yes you can put all of your javascript code in 1 or more js files. – Vadim Nov 24 '14 at 15:59
  • @Vadim It's true, we can not visit many excellent sites in my country, like google, facebook, twitter, etc. I just fixed the question. – Qi Tong Nov 24 '14 at 16:02
  • @JoaoLeal I just fixed my question, I just wonder if the angular can define a ng-click function only in js, not in html, like jquery. – Qi Tong Nov 24 '14 at 16:09
  • yes, you can just use angular.element and do a .bind('click' – SoluableNonagon Nov 24 '14 at 16:16

1 Answers1

0

Yes, in a directive would be best

http://plnkr.co/edit/RCcrs5?p=preview

myApp.directive("button", function(){

  return{
      restrict: 'A',
      link: function(scope , element){
         element.bind("click", function(e){
            // do something
         });
      }
  }
})
SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98