Hans Daigle Hans Daigle - 1 year ago 44
AngularJS Question

How can I ng-click a function from my angular-js directive?

I cant figure how to call a function from my directive using ng-click.

This is an example of my HTML:

<directive-name data="exemple">
<button class=btn btn-primary type="submit" ng-click="search()">

This is an example of my javascript :

.directive('directiveName', ['exempleService', function(exempleService) {
function link(scope, element, attrs) {

scope.search = function() {
console.log("this is working")

return {
link: link,
restrict: 'E',
scope: {data:'=',


Thanks in advance! :)

Answer Source

You have to create a directive which includes the button inside the directive template (either as HTML or external file).


<div ng-app="app">
  <directive-name data="exemple"></directive-name>


var app = angular.module('app', [])

app.directive('directiveName', function() {
  return {
    restrict: 'E',
    link: function(scope, element, attrs) {
      scope.search = function() {
    template: '<button class=btn btn-primary type="submit" ng-click="search()">CLICK</button>'