[英]AngularJS execute controller function in directive template
I made a custom directive with AngularJS, and in the template I called a function in controller, but it didn't work. 我用AngularJS做了一个自定义指令,在模板中我在控制器中调用了一个函数,但是没有用。
thanks for your help :) 谢谢你的帮助 :)
<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>
You should place your directive inside your controller wrapper like below. 您应该将指令放入控制器包装中,如下所示。
<div ng-controller="myCtrl">
<ng-selectbox my-function="myfunction()" items="codes"></ng-selectbox>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.