我已经设置了一个指令来处理在AngularJS中加载jQuery Flot图表:
var Dashboard = angular.module('StatsDashboard', []);
Dashboard.directive('chart', function() {
return {
restrict: 'E',
template: '<div></div>',
replace: true,
link: function(scope, elem, attrs) {
var chart = null;
var data = scope[attrs.ngModel];
var options = {}
$.plot(elem, data, options);
elem.show();
}
};
});
该指令从控制器中加载:
function ChartCtrl($scope, $http)
{
$http.get('controllers/weekly_overview_charts.php').success(function(data) {
$scope.reg_vs_act = data;
});
}
我遇到了一个"TypeError: Cannot read property 'length' of undefined at parseData"的错误。
我确认了这是由于http函数的异步特性导致的。
我的问题是,我该如何确保数据在指令尝试加载flot图表之前被返回?
谢谢!