[英]How can I ng-click a function from my angular-js directive?
我无法弄清楚如何使用ng-click从指令中调用函数。
这是我的HTML的示例:
<div>
<directive-name data="exemple">
<button class=btn btn-primary type="submit" ng-click="search()">
</directive-name>
</div>
这是我的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:'&'}
}
}]);
提前致谢! :)
您必须创建一个directive
,其中包括指令模板内的button
(HTML或外部文件)。
视图
<div ng-app="app">
<directive-name data="exemple"></directive-name>
</div>
指示
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>'
}
})
您的按钮应该在指令模板中
.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:'&'}
}
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.