Angularjs指令获取异步的$scope值
我对Angularjs是新手。我想获取自定义指令的属性,进行http调用,并使用响应生成图形Angularjs指令获取异步的$scope值,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我对Angularjs是新手。我想获取自定义指令的属性,进行http调用,并使用响应生成图形 <lineGraph query="select * from load.webserver[0-9]"></lineGraph> <!-- query is sent over http to db --> 我的angularjs代码是这样的 var app = angular.module('app', []); //Factory to get dat
<lineGraph query="select * from load.webserver[0-9]"></lineGraph> <!-- query is sent over http to db -->
我的angularjs代码是这样的
var app = angular.module('app', []);
//Factory to get data from influxDB over http
app.factory('influxFactory', ['$q', function($q) {
.... code for querying db using http
})
//
app.controller('mycontroller', [ '$scope', 'influxFactory', function($scope, influxFactory){
scope.constructGraphData = function(query){
influxFactory.getDataFromDBOverHTTP().then(function(data){
... graph data is constructed here
$scope.graphdata = constructed_data // graph data to be plotted is stored and need to be given to drawFlotChart
})
}
})
app.directive('lineGraph', function () {
function drawFlotChart(graphdata){
... flot code to draw the graph
}
return {
restrict: 'E',
templateUrl: 'templates/linegraph.html',
link: function(scope, elt, attr){
// Take value of "query" in <myLineDirective query="load.we...
scope.constructGraphData(attr.query)
/*** How do I capture the $scope.graphdata here ... its "undefined" since http response takes time. Need help. ***/
}
})
var-app=angular.module('app',[]);
//工厂通过http从XDB获取数据
app.factory('influxFactory',['$q',function($q){
..使用http查询数据库的代码
})
//
app.controller('mycontroller',['$scope','influxFactory',function($scope,influxFactory){
scope.constructGraphData=函数(查询){
influxFactory.getDataFromDBOverHTTP().then(函数(数据){
…图形数据在此构造
$scope.graphdata=constructed\u data//要绘制的图形数据已存储,需要提供给drawFlotChart
})
}
})
app.directive('lineGraph',函数(){
函数drawFlotChart(图形数据){
…使用flot代码绘制图形
}
返回{
限制:'E',
templateUrl:'templates/linegraph.html',
链接:功能(范围、elt、属性){
//在中获取“query”的值您可以在控制器的constructGraphData
函数中使用$q
。在函数开头创建一个延迟对象,返回其承诺,并在准备就绪时使用constructGraphData
解析。然后,在指令中调用scope.constructGraphData(attr.query)。然后(…)
要使用图形数据
可以执行以下操作:。注意:我使用$timeout替换$http,但其工作方式应相同。