我们正在创建一个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(app模块)
以下是应用模块: -
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()));
}
});
});
另一种方法可能是合并两个模块。
感谢。
答案 0 :(得分:0)
你可以通过多种方式做到这一点,但最干净(松散耦合的方式)就是事件。
如果模块加载在同一页面上,则意味着它们至少是$ rootScope的子项,您可以在$ rootScope上监听和引发事件以传达这些信息($ rootScope。$ on(' myEvent& #39;,function(data){}) - $ rootScope。$ emit(' myEvent',{...})。
一个不太清晰的解决方案是拥有某种数据管理器(AngularJS服务),您可以在其中存储此信息并在多个位置进行提取。然后接收器控制器将观察此service.getLogs()的响应,并且在更改时您将获得最新数据(这在性能方面有点难度,因此不是真的建议)。
编辑:如果您需要在信息到达时显示特定的信息块,您可以使用null标记接收器控制器数据模型(" $ scope.myModel.logs")在应该使用myModel.logs显示这些日志的块上添加ng-if条件(当接收到事件时,将更新数据模型并显示块)
答案 1 :(得分:0)
你可以查看我之前发布的一篇文章 -
http://stackoverflow.com/questions/25301581/angular-js-newbie-link-in-a-controller-view-that-triggers-another-controller-a/25305951#25305951
我添加了一个plunk作为演示 - http://plnkr.co/edit/1dhdPfmB1WwAkYhsz4Hv