javascript - AngularJS在指令模板中执行 Controller 函数

标签 javascript angularjs directive

我用 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/

相关文章:

javascript - 我应该如何在javascript中循环遍历and "associative object"?

javascript - 将@连接到 Angular 表达式

javascript - AngularJS $scope 在模板中无法访问

javascript - AngularJS ng-model 表单由 ng-repeat 驱动的 UI 模型描述数据如何?

javascript - javascript window.print() 的 Safari 打印问题

javascript - 你能指定页面元素的加载顺序吗?

javascript - 在 HTML5 移动应用程序中实现数据库

javascript - $scope.$watch 上的 ng-class 在第二次单击时不起作用

javascript - 是什么导致 AngularJS 变量更改触发 html 更新?

javascript - Angular Directive(指令)不看