绑定 AngularJS 与 JavaScript 插件

Binding AngularJS with JavaScript Plugin

我正在尝试将 AngularJS 与 JavaScript 插件 - OpenSeaDragon 绑定,因为我必须在 OpenSeaDragon 的图像上做注释,我尝试了类似的方法

angular.element(document.body).scope().$on('addAnnotation', function(e, data){    
    self._viewer.viewport.fitBounds(new OpenSeadragon.Rect(data)) 
});

控制器内部:

$scope.addAnnotation= function (data) { 
   $scope.$root.$emit('addAnnotation', data);
}

在我看来:

ng-click="addAnnotation(data)"

但我收到错误

Uncaught TypeError: Cannot read property '$on' of undefined

您正在尝试获取 angular.element(document.body).scope(),即 undefined。 为了得到一个元素的scope,这个元素应该有一个作用域。 尝试在绑定到控制器的 DOM 元素上使用它。