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


i cant figure how call function directive using ng-click.

this example of html:

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

this example of javascript :

... .directive('directivename', ['exempleservice', function(exempleservice) {     function link(scope, element, attrs) {          scope.search = function() {             console.log("this working")         };     };      return {         link: link,         restrict: 'e',         scope: {data:'=',         search:'&'}     }  }]); 

thanks in advance! :)

you have create directive includes button inside directive template (either html or external file).

view

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

directive

var app = angular.module('app', [])  app.directive('directivename', function() {   return {     restrict: 'e',     link: function(scope, element, attrs) {       scope.search = function() {         alert('boe');       }     },     template: '<button class=btn btn-primary type="submit" ng-click="search()">click</button>'    } }) 

fiddle


Comments