AngularJS集成Highcharts遇Error #13问题求助
Hey there, let's break down exactly why you're hitting this frustrating Highcharts Error #13, and why moving your initialization code fixes it.
First, let's clarify what Highcharts Error #13 means: it's Highcharts telling you it can't find the DOM element you specified to render the chart into. That weird console message ("Render to element with ID : 0,Number of matching dom elements : tagIDProj1") is a key clue—when your code runs in the "inside function - 2" block, Highcharts isn't actually seeing the tagIDProj1 element in the DOM yet.
The Core Issue: DOM Rendering Timing & AngularJS Lifecycle
AngularJS doesn't render your template to the DOM immediately when the controller loads. It waits until its digest cycle completes—this is when it resolves all template bindings (like dynamic IDs, ng-if conditions, or ng-repeat loops) and updates the DOM.
Here's what's happening in your case:
- When you put the Highcharts initialization in the "inside function - 2" block, that function is likely executing before AngularJS finishes rendering the
tagIDProj1element to the DOM. Maybe the function runs right when the controller initializes, or it's triggered before the digest cycle wraps up. Highcharts looks for the element, can't find it, and throws Error #13. - When you move the code to the "inside main scope - 1" (the top-level of your controller), you're getting lucky with timing: the controller's top-level code runs just as AngularJS finishes its initial digest cycle, so the
tagIDProj1element exists in the DOM when Highcharts tries to use it.
Common Scenarios That Worsen This
If your tagIDProj1 element is inside an ng-if, ng-repeat, or another directive that delays DOM rendering, this problem gets even more pronounced. Those directives only add elements to the DOM when their conditions are met—if your function runs before that condition is resolved, the element won't exist.
Fixes to Make This Reliable
Don't rely on lucky timing—use these AngularJS-friendly approaches to ensure Highcharts only initializes when the DOM is ready:
Use
$timeoutto Delay Initialization
Wrap your Highcharts call in$timeout(without a delay value) to let AngularJS finish its digest cycle first:// Inside your "inside function - 2" block $timeout(function() { Highcharts.chart('tagIDProj1', { // Your Highcharts config here }); });$timeoutqueues the code to run after the next digest cycle, guaranteeing the DOM is updated.Create a Custom Directive (Best Practice)
For long-term maintainability, wrap Highcharts in an AngularJS directive. Directives run theirlinkfunction after the element is rendered to the DOM, so you avoid timing issues entirely:angular.module('yourApp').directive('projectChart', function() { return { restrict: 'E', scope: { chartConfig: '=' }, link: function(scope, element) { // Use the directive's element directly instead of an ID Highcharts.chart(element[0], scope.chartConfig); } }; });Then in your template:
<project-chart chart-config="yourChartConfig"></project-chart>This eliminates the need to rely on IDs entirely, which is cleaner and avoids conflicts.
Verify Dynamic IDs (If Applicable)
IftagIDProj1is a dynamic ID (e.g.,id="{{someProjectId}}"), make sure the variable holding the ID is fully resolved before initializing Highcharts. Using$timeoutor a directive will handle this automatically, since they wait for bindings to resolve.
内容的提问来源于stack exchange,提问作者SurajK

