Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have run into a scoping problem when creating click functions on the nodes of my graphs built using Angularjs, Highcharts and the highcharts-ng directive.

The problem I am having is that any angular directives or functionality I place in my click function does not work.

Here is a simplified version of my problem - http://jsfiddle.net/47m46/8/

Click on any graph node to see the click function being called. Clicking on the close button should fire an ng-click event which loads up a simple function that displays an alert

This is the html that my click function generates:

<div ng-controller="myctrl">
    <button ng-click="closeDrillDown()">Close</button>
    <h2>Positive Sentiment</h2>
    <p class="drillDownInfo"><strong>20%</strong> of posts where we can determine a sentiment are Positive</p>
</div>

This is the function called with ng-click which does not get triggered

$scope.closeDrillDown = function() {
      alert('function works');
    };

The author of the highcharts-ng directive mentioned in the following thread https://github.com/pablojim/highcharts-ng/issues/56 that "These events (click function) will be outside of the angular world though so you'll need to wrap them with an apply or similar."

So my question is: How do I give my highcharts click function access to Angularjs? I have tried to use $scope.$apply() but my limited knowleadge of this facet of angular has not given me any results.

share|improve this question
    
Try this jsfiddle.net/themyth92/47m46/10 – themyth92 Jan 21 '15 at 9:47
    
Wow thanks man! I think this has given me enough information to carry on from here. – ocajian Jan 21 '15 at 9:56
    
Maybe instead of trigger ng-click, use jquery and run a function. jsfiddle.net/47m46/11 – Sebastian Bochan Jan 21 '15 at 10:27
    
Hi Sebastian this is not really an option for me as I would like to use Angular functionality inside of the click function – ocajian Jan 21 '15 at 10:53
    
I introduce you only workaround ;) – Sebastian Bochan Jan 22 '15 at 12:40

As stated in the comments above, the solution to this question would be the following:

http://jsfiddle.net/themyth92/47m46/10/

var content = '<div id = "postListPopUpContainer"><div id = "postListPopUp"><div><button ng-click="closeDrillDown()">Close</button><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></div></div></div>';

$('#myCtrl').append($compile(content)($scope));

The part to take note of would be where we use $compile(). This places the generated html into the Angularjs context.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.