我看到 Angular 有两种设置 Controller 的方法。 旧的:
app.controller('my_ctrl', ['$scope', function($scope){
$scope.a = null;
}]);
还有这个:
app.controller('my_ctrl',function(){
this.a = null;
});
现在我尝试使用 $on 监听器并在文档中使用 $scope。我试着像这样使用它:
this.$on
但是我有这个错误:
this.$on is not a function
请问我如何以第二种方式使用 $on?
最佳答案
您使用 $on
的方式是在 $scope
变量本身而不是 this
指针上使用它。
试试这个。
$scope.$on('listener', function() {})
以你的例子为例
app.controller('my_ctrl', function($scope) {
$scope.$on('listener', function() {
// do stuff on the event
})
})
关于javascript - Angular js 设置在没有 $scope 的情况下使用 $on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515757/