阿波羅的戰車
2018-10-12 14:15:45
請問如何在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: '<input ng-click="test()"></input>', scope:{ test: '&' }, controller: [ '$scope', function ($scope) { $scope.accept = function () { $scope.test(); }; } ], } });html是這樣的:<button type="button" ng-click="accept()">test</button>要怎樣才能實現點擊test button的時候在調用了directive里的accept function之后同時調用controller里面的test function呢?
1 回答

拉風的咖菲貓
TA貢獻1995條經驗 獲得超2個贊
var app = angular.module('myApp',[]);
app.controller('myCtrl',function($scope){
$scope.accept= function(){
...
}
}
app.directive('myDir',
function(){
return {
restrict:'EA',
replace: true,
template: '<input ng-click="test()"></input>',
scope:{
test: '&'
},
link: function(scope, element, attrs) {
元素.bind("click",function(){
scope.accept();
})
}
}
}
);
你看看可不可以這樣子弄
添加回答
舉報
0/150
提交
取消