[英]Ionic pass array to service
I have an Ionic application which allows the user to perform a search, I save the results from the search into a service and if that page is visited again and there is a result in the service from before the user should be redirected to the results page where the user can click to clear results to restart search. 我有一个Ionic应用程序,该应用程序允许用户执行搜索,我将搜索结果保存到服务中,如果再次访问该页面,并且服务中存在结果,则应将用户重定向到结果页面之前用户可以单击此处以清除结果以重新开始搜索。
My current code shows the data on the results page but when the click to reset is clicked it doesn't do anything, I am passing the results directly from my API to the $state so I do bypass the service but aswell as passing to the $state I also save the data to the service. 我当前的代码在结果页面上显示了数据,但是当单击“重置”时,它没有任何作用,我将结果直接从我的API传递到$ state,因此我确实绕过了该服务,但也传递给了$ state我也将数据保存到服务中。
Pseudo Code*** 伪代码***
my injected controller dependencies 我注入的控制器依赖项
.controller('SearchCtrl', function($scope, $http, $state, $ionicLoading, $ionicHistory, serviceName) {
if(serviceName.searchHistory){
$state.go('app.searchResults', {items: {items: serviceName.getSearch}});
} else {
$scope.beginSearch = function() {
$http.post('https://domain/api/v1/search?token=' + localStorage.getItem("token"),$scope.search).then(function(successResponse){
$scope.return = successResponse;
$scope.searchResponse = $scope.return.data.data[0];
serviceName.setSearch($scope.searchResponse.items);
$state.go('app.searchResults', {items: {items: $scope.searchResponse.items}});
});
}
}
})
my service 我的服务
angular.module('starter.services', [])
.factory('serviceName', function() {
var searchHistory = [];
return {
getSearch: function(){
return searchHistory;
},
setSearch: function(data){
var searchHistory = data;
},
clearSearch: function(){
searchHistory = [];
}
}
})
My results controller 我的结果控制器
.controller('resultsCtrl', function($scope, $http, $state, $ionicLoading, $stateParams, $ionicPopup, serviceName) {
$scope.item = $stateParams.items.items;
$scope.data = {};
$scope.resetFilter = function(){
serviceName.clearSearch();
$state.go('app.beginSearch');
}
})
You should not use an Array as $stateParameters, they work like a query string parameters. 您不应将Array用作$ stateParameters,它们的工作方式类似于查询字符串参数。
And alternative for what you want to achieve could be this one: 而您想要实现的替代方案可能是:
Redirect the user to the 'app.searchResults'
state. 将用户重定向到'app.searchResults'
状态。
.controller('SearchCtrl', function($scope, $http, $state, $ionicLoading, $ionicHistory, serviceName) {
if(serviceName.searchHistory){
$state.go('app.searchResults');
} else {
$scope.beginSearch = function() {
$http.post('https://domain/api/v1/search?token=' + localStorage.getItem("token"),$scope.search).then(function(successResponse){
$scope.return = successResponse;
$scope.searchResponse = $scope.return.data.data[0];
serviceName.setSearch($scope.searchResponse.items);
$state.go('app.searchResults', {items: {items: $scope.searchResponse.items}});
});
}
}
})
And in its controller, load the result from your custom service. 并在其控制器中加载您的自定义服务的结果。
.controller('resultsCtrl', function($scope, $http, $state, $ionicLoading, $stateParams, $ionicPopup, serviceName) {
$scope.item = serviceName.getSearch();
$scope.data = {};
$scope.resetFilter = function(){
serviceName.clearSearch();
$state.go('app.beginSearch');
}
})
UPDATE: 更新:
In your service your are using var
inside your update method, what means that you are creating a new reference of searchHistory . 在您的服务中,您正在update方法中使用var
,这意味着您正在创建searchHistory的新引用。 You should remove the var in order to keep the reference. 您应该删除var以便保留引用。
angular.module('starter.services', [])
.factory('serviceName', function() {
var searchHistory = [];
return {
getSearch: function(){
return searchHistory;
},
setSearch: function(data){
searchHistory = data;
},
clearSearch: function(){
searchHistory = [];
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.