我正在尋找運行從REST端點收集數據的外部JS函數,因此需要花費少量時間來執行此操作。這些數據將被插入圖表中,但圖表正試圖在收集數據之前加載。在AngularJS控制器中調用外部函數
外部JS:
function callEndpoint() {
var sensorID = document.getElementById('sensorList').value;
$.getJSON('http://193.61.148.125:443/SensorCentral/REST/SensorDataRangeNanos/19455746_3_10?startTs=0&endTs=9000000000000000000',
function(data) {
globalData = data;
});
}
我root.js:
angular.module('root',['AngularChart'], function($routeProvider, $locationProvider){
$routeProvider.when('/',{
template: '<chart title="Bedroom Sensors" xData="lineChartXData" yData="lineChartYData" xName="Month" yName="Activations" subtitle="Sensor Usage"></chart>',
controller: MainCtrl
})
})
我在使用服務的嘗試:
angular.module('root',['AngularChart'], function($routeProvider, $locationProvider){
service('myService', function() {
this.callAlert = function() {
callEndpoint();
}});
$routeProvider.when('/',{
template: '<chart title="Bedroom Sensors" xData="lineChartXData" yData="lineChartYData" xName="Month" yName="Activations" subtitle="Sensor Usage"></chart>',
controller: MainCtrl
})
})
編輯:
我訪問globalData
雖然MainCtrl
函數調用(popGraph()
):
function MainCtrl($scope, $http){
callEndpoint();
var data = {"xData": ["Mon", "Tues", "Wed", "Thurs", "Fri", "Sat","Sun"],"yData":[{
"name": "Door",
"data": [7.0, 6.9, 9.5, 14.5, popGraph(), 21.5, 25.2]
}, {
"name": "Drawer",
"data": [2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8]
}, {
"name": "Cupbaord 1",
"data": [9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6]
}, {
"name": "Cupboard 2",
"data": [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0]
}]}
$scope.lineChartYData=data.yData
$scope.lineChartXData=data.xData
}
,您是否試圖使用'resolve'? – Pradeepb
嘗試承諾:https://api.jquery.com/jquery.when/ –
@Pradeepb您可以協助語法嗎? – Colin747