我正在使用https://github.com/johnpapa/angular-styleguide约定。
在部分隐藏实现细节的函数声明
当我将其绑定(bind)到 this
时,如何在 side html 中使用函数,如下所示:
function AvengersCtrl() {
var vm = this;
vm.activate = activate;
function activate() {
alert("activate");
}
}
并在 html 中使用它:
<body ng-controller="AvengersCtrl">...
<button ng-click="activate()"></button>
</body>
当我使用scope
而不是this
时,它可以工作
$scope.activate = activate;
最佳答案
您需要使用所谓的 controller as语法:
<body ng-controller="AvengersCtrl as ctrl">...
<button ng-click="ctrl.activate()"></button>
</body>
关于javascript - 如何在html中使用绑定(bind)函数到 "this"?风格指南 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32369430/