我用 AngularJS 制作了一个自定义指令, 在模板中我在 Controller 中调用了一个函数, 但没有成功。
感谢您的帮助:)
<div ng-contorller="myCtrl">
<ng-selectbox my-function="myfunction()" items="codes"></ng-selectbox>
</div>
myapp.controller("myCtrl", function($scpoe){
$scope.myfunction= function(){
alert("123");
};
});
myapp.directive("ngSelectbox", function(){
return {
restrict: "E",
scope: {
items: "=",
myfunction: "&"
},
template:
"<div id='selectbox'>" +
"<ul ng-repeat='item in items'>" +
"<li ng-click='myfunction()'>{{item.TYPE}}</li>" +
"</ul>" +
"</div>"
};
});
最佳答案
不要在使用指令的地方添加调用括号,只需像这样使用 <ng-selectbox my-function="myfunction" items="codes"></ng-selectbox>
关于javascript - AngularJS在指令模板中执行 Controller 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29275263/