所以我有一个小角度应用程序,它接收一个搜索查询,将它发送到我已经设置的弹性搜索节点,然后在屏幕上显示结果集。
我的问题是,当我进行新查询时,结果会附加到当前结果集的末尾。我希望它能够删除当前页面上的任何内容,并仅使用新数据重新加载它,就像在Google上搜索某些内容会返回一组全新的结果一样。
有没有办法做到这一点?以下代码供参考。
// this is the controller that displays the reuslts.
var displayController = function($scope, $rootScope, $window, notifyingService) {
var dataReady = function(event, data) {
$scope.resultSet = notifyingService.getData();
}
$rootScope.$on('data-ready', dataReady)
}
app.controller("displayController", ["$scope", "$rootScope", "$window", "notifyingService", displayController]);
// this is the service that's responsible for setting the data
var notifyingService = function($http, $rootScope) {
var svc = {
_data: [],
setData: setData,
getData: getData
};
function getData() {
return svc._data;
}
function setData(data) {
var base_obj = data.hits.hits
console.log("Setting data to passed in data.");
console.log('length of dataset: ' + base_obj.length);
for(var i = 0; i < base_obj.length; i++){
svc._data.push(base_obj[i]._source);
}
$rootScope.$broadcast('data-ready', svc._data);
}
return svc;
};
app.factory("notifyingService", ["$http", "$rootScope", notifyingService]);
答案 0 :(得分:1)
在循环重新初始化svc._data
之前的setData中svc._data = [];
答案 1 :(得分:0)
在开始添加新查询之前清除svc._data。
function setData(data) {
var base_obj = data.hits.hits;
svc._data = [];//reset your array before you populate it again.
for(var i = 0; i < base_obj.length; i++){
svc._data.push(base_obj[i]._source);
}
$rootScope.$broadcast('data-ready', svc._data);
}