我们正在创建一个Telecom App仪表板。我们试图使用Logstash和Elastic搜索获取日志,并使用Angularjs的ng-Table指令在UI上显示它。在不同模块的控制器之间传递数据
我们能够获取日志,但问题是在两个不同模块的控制器之间发送响应。
下面是代码,
对于从弹性搜索检索日志:
// We define an EsConnector module that depends on the elasticsearch module.
var EsConnector = angular.module('EsConnector', ['elasticsearch']);
// Create the es service from the esFactory
EsConnector.service('es', function (esFactory) {
return esFactory({ host: 'localhost:9200' });
});
// We define an Angular controller that returns the server health
// Inputs: $scope and the 'es' service
EsConnector.controller('ServerHealthController', function($scope, es) {
es.cluster.health(function (err, resp) {
if (err) {
$scope.data = err.message;
} else {
$scope.data = resp;
}
});
});
// We define an Angular controller that returns query results,
// Inputs: $scope and the 'es' service
EsConnector.controller('QueryController', function($scope, es) {
// search for documents
es.search({
index: 'logstash-2014.08.29',
size: 500,
body: {
"query":
{
"match": {
"CallId":-1 }
},
}
}).then(function (response) {
$scope.hits = response.hits.hits;
});
});
我们需要通过数据,即从QueryController获得命中(的EsConnector模块)至MainController(表观的模块)
这里是应用程序模块: -
var app = angular.module('SnapshotApp',['ngTable']);
app.controller('MainController', function($scope, ngTableParams){
$scope.query = {}
$scope.queryBy = '$'
var data = ; \\ we want to populate 'data' with 'hits' of QueryController
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: data.length, // length of data
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
});
另一种方法可能是合并两个模块。
谢谢。
这是一个模块内的两个控制器。如果我们想在两个模块之间传输数据会怎样? – 2014-09-02 09:09:09