I cant figure how to call a function from my directive using ng-click.
This is an example of my HTML:
<div>
<directive-name data="exemple">
<button class=btn btn-primary type="submit" ng-click="search()">
</directive-name>
</div>
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:'=',
search:'&'}
}
}]);
Thanks in advance! :)
You have to create a directive
which includes the button
inside the directive template (either as 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>'
}
})
Your button should be in your directive template
.directive('directiveName', ['exempleService', function(exempleService) {
function link(scope, element, attrs) {
scope.search = function() {
console.log("this is working")
};
};
return {
link: link,
restrict: 'E',
template: '<div><button class=btn btn-primary type="submit" ng-click="search()"></div>'
scope: {data:'=',
search:'&'}
}
}]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.