我已经设置了一个指令来处理使用 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();
}
};
});
该指令从 Controller 加载:
function ChartCtrl($scope, $http)
{
$http.get('controllers/weekly_overview_charts.php').success(function(data) {
$scope.reg_vs_act = data;
});
}
我收到一个“TypeError:无法读取未定义的属性“长度” 在parseData”。
这肯定是由于 http 函数的异步性质造成的。
我的问题是,如何确保在指令尝试加载流程图之前返回数据?
谢谢!
最佳答案
以及我自己的问题的答案:
scope.$watch(attrs.ngModel, function(v){
if(v)
{
chart = $.plot(elem, v, options);
elem.show();
}
});
这是触发范围。$注意:
var data = scope[attrs.ngModel];
在本例中应该只是 attrs.ngModel。
关于jquery - AngularJS + jQuery Flot + 使用 http.get 从 Controller 加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17460669/