请问如何在directive里面调用controller的function?代码如下:
var app = angular.module('myApp',[]);
app.controller('myCtrl',function($scope){
$scope.test= function(){
...
}
}
app.directive('myDir',
function(){
return {
restrict:'EA',
replace: true,
template: '',
scope:{
test: '&'
},
controller: [
'$scope', function ($scope) {
$scope.accept = function () {
$scope.test();
};
}
],
}
}
);
html是这样的:
要怎样才能实现点击test button的时候在调用了directive里的accept function之后同时调用controller里面的test function呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你看看可不可以这样子弄
可以把
myCtrl里的test function存到service里,然后在directive里调用service里的test function或者:
把你的列子写进在线测试里面了
https://plnkr.co/edit/hX6Rwj1...
刚好之前回答过类似你这样的问题
之前写的在线例子:
http://embed.plnkr.co/SirYJd5...
<button type="button" ng-click="accept()">test</button>这是html里的调用方式?然后这个是 directie 的 template:
<input ng-click="test()"></input>?所以你的
myDir这个 directive 到底是在哪儿调用的?另外,如果方便,请你大概描述一下你要实现什么功能。