我为我的图形设置了一个工厂提供程序。该图是从highcharts和highcharts-ng构建的,这是一个angularjs highcharts指令。
然后,在名为SentimentChartController的控制器中,我在图形节点上创建了一个单击函数,如下所示:
$scope.AveSentimentChartConfigMain.options.plotOptions.series.point.events.click = function () {
var containerDiv = document.createElement('div');
containerDiv.setAttribute('id', 'postListPopUpContainer');
containerDiv.setAttribute('ng-controller', 'SentimentChartController');
var contentDiv = document.createElement('div');
contentDiv.setAttribute('id', 'postListPopUp');
contentDiv.innerHTML = '<img onclick="closeDrillDown()" src="images/closePostListPopUp.svg" title="Click to close" class="closeIcon" width="18" height="18"><h2>' +
this.category + ' ' + this.series.name + '</h2>' +
'<p class="drillDownInfo"><strong>' + this.y + '%</strong> of posts where we can determine a sentiment are ' + this.category + '</p>';
document.body.appendChild(containerDiv);
containerDiv.appendChild(contentDiv);
};单击图形节点后生成的html如下所示:
<div id="postListPopUpContainer" ng-controller="SentimentChartController">
<div id="postListPopUp">
<img src="images/closePostListPopUp.svg" title="Click to close" class="closeIcon" width="18" height="18" onclick="closeDrillDown()">
<h2>Positive Sentiment</h2>
<p class="drillDownInfo"><strong>26%</strong> of posts where we can determine a sentiment are Positive</p>
</div>
</div>在我的SentimentChartController中,我还声明了以下函数:
$scope.closeDrillDown = function() {
alert('function called');
};此函数应在上面定义的my click函数期间创建的以下img元素的onclick上触发
<img onclick="closeDrillDown()" src="images/closePostListPopUp.svg" title="Click to close" class="closeIcon" width="18" height="18">然而,我得到了一个“未捕获ReferenceError:未定义closeDrillDown”错误。
为什么即使我在容器div中设置了ng-controller="SentimentChartController“,也会发生这种情况?
发布于 2015-01-19 15:54:17
对于限定了作用域的函数,您应该使用ng-click属性而不是onclick,您收到了这个错误,因为您的函数是在您的作用域中定义的,而不是作为全局javascript函数定义的。另外,在控制器中选择dom不是一个好的做法,请尝试使用angular.element在单独的指令中进行此dom选择。
https://stackoverflow.com/questions/28019846
复制相似问题