3 回答

TA貢獻1772條經驗 獲得超5個贊
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();
})
}
}
}
);

TA貢獻1843條經驗 獲得超7個贊
可以把myCtrl
里的test function
存到service
里,然后在directive
里調用service
里的test function
app.controller('myCtrl',function($scope,methods){
$scope.test= function(){
...
}
methods.test = $scope.test;
}
app.factory('methods',function(){
return {
test: null
};
})
app.directive('myDir',
function(){
return {
restrict:'EA',
replace: true,
template: '<input ng-click="accept()"></input>',
scope:{
},
controller: function ($scope,methods) {
$scope.accept = function () {
console.log('accept fn');
methods.test();
};
}
}
}
);
或者:
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: function(){}
}
});
HTML:
<my-dir test="test()"></my-dir>

TA貢獻1893條經驗 獲得超10個贊
<button type="button" ng-click="accept()">test</button>
這是 html
里的調用方式?
然后這個是 directie 的 template: <input ng-click="test()"></input>
?
所以你的 myDir
這個 directive 到底是在哪兒調用的?另外,如果方便,請你大概描述一下你要實現什么功能。
- 3 回答
- 0 關注
- 1066 瀏覽
添加回答
舉報