我們正在創建一個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