在过滤器函数中根据来自本地存储的数据进行过滤器
make filter based on data from localstorage in the filter function
我是 Ionic-angular.js 的新手,希望有人能帮我解决这个问题
首先,这是代码
favorites.html
...
<ion-item ng-repeat="dish in dishes | favoriteFilter:favorites" href="#/app/menu/{{dish.id}}" class="item-thumbnail-left" on-swipe-left="deleteFavorite(dish.id)">
<img ng-src="{{baseURL+dish.image}}" on-swipe-left="deleteFavorite(dish.id)">
<h2>{{dish.name}}
<ion-delete-button class="ion-minus-circled"
ng-click="deleteFavorite(dish.id)">
</ion-delete-button>
</ion-item>
...
services.js
.factory('favoriteFactory', ['$resource', 'baseURL', function ($resource, baseURL) {
var favFac = {};
var favorites = [];
favFac.addToFavorites = function (index) {
for (var i = 0; i < favorites.length; i++) {
if (favorites[i].id == index)
return;
}
favorites.push({id: index});
};
favFac.deleteFromFavorites = function (index) {
for (var i = 0; i < favorites.length; i++) {
if (favorites[i].id == index) {
favorites.splice(i, 1);
}
}
}
favFac.getFavorites = function () {
return favorites;
};
return favFac;
}])
.factory('$localStorage', ['$window', function($window) {
return {
store: function(key, value) {
$window.localStorage[key] = value;
},
get: function(key, defaultValue) {
return $window.localStorage[key] || defaultValue;
},
storeObject: function(key, value) {
$window.localStorage[key] = JSON.stringify(value);
},
getObject: function(key,defaultValue) {
return JSON.parse($window.localStorage[key] || defaultValue);
}
//removeItem: function(key){
// $window.localStorage.removeItem(key);
//}
}
controller.js
.filter('favoriteFilter', 'localStorage', function (localStorage) {
if(localStorage.getItem('favorites')!=undefined)
{
var out = [];
return out;
}
else{
return function (dishes) {
var old_favorite = JSON.parse($localStorage.get('favorites'));
var leng = Object.keys(old_favorite).length;
console.log(leng);
var out = [];
for (var i = 0; i < leng; i++) {
for (var j = 0; j < dishes.length; j++) {
if (dishes[j].id === favorites[i].id)
out.push(dishes[j]);
}
}
return out;
}}
});
举个例子,localstorage里面有一个这样的数组
Key : favorites
value : [{"id":1},{"id":2},{"id":0}]
所以,逻辑是,我根据ID和过滤函数比较来自数据库和本地存储的ID
如果ID相同,则数据库中的数据将其推入收藏夹菜单。
但是,它无法显示在收藏夹菜单中,当我在控制台上查看时,它说
[ng:areq] Argument 'fn' is not a function, got string
我在这里做错了吗?还是我放错方法了?
提前致谢。
您提供的错误似乎是语法问题。您缺少数组括号。
.filter('favoriteFilter', ['$localStorage', function (localStorage) {
if(localStorage.getItem('favorites')!=undefined)
{
var out = [];
return out;
}
else
{
return function (dishes) {
var old_favorite = JSON.parse($localStorage.get('favorites'));
var leng = Object.keys(old_favorite).length;
console.log(leng);
var out = [];
for (var i = 0; i < leng; i++) {
for (var j = 0; j < dishes.length; j++) {
if (dishes[j].id === favorites[i].id)
out.push(dishes[j]);
}
}
return out;
}
};
}]);
我没有检查你的逻辑功能,这将是解决你错误的答案。
尝试不同的方法:
由于您已经拥有 addToFavorites 和 deleteFromFavorites 功能,您只需按照以下 3 个步骤操作即可:
定义你的'favorites'数组时,只需按如下方式赋值即可:
var 收藏夹 = JSON.parse(window.localStorage['favorites'] || []);
在您的 addToFavorites 函数中,将添加的项目推送到数组后,添加:window.localStorage['favorites'] = JSON.stringify(favorites);
在 deleteFromFavorites 函数中,拼接数组后,添加:window.localStorage['favorites'] = JSON.stringify(favorites);
这三个超级简单的步骤你应该会很好!
我是 Ionic-angular.js 的新手,希望有人能帮我解决这个问题
首先,这是代码
favorites.html
...
<ion-item ng-repeat="dish in dishes | favoriteFilter:favorites" href="#/app/menu/{{dish.id}}" class="item-thumbnail-left" on-swipe-left="deleteFavorite(dish.id)">
<img ng-src="{{baseURL+dish.image}}" on-swipe-left="deleteFavorite(dish.id)">
<h2>{{dish.name}}
<ion-delete-button class="ion-minus-circled"
ng-click="deleteFavorite(dish.id)">
</ion-delete-button>
</ion-item>
...
services.js
.factory('favoriteFactory', ['$resource', 'baseURL', function ($resource, baseURL) {
var favFac = {};
var favorites = [];
favFac.addToFavorites = function (index) {
for (var i = 0; i < favorites.length; i++) {
if (favorites[i].id == index)
return;
}
favorites.push({id: index});
};
favFac.deleteFromFavorites = function (index) {
for (var i = 0; i < favorites.length; i++) {
if (favorites[i].id == index) {
favorites.splice(i, 1);
}
}
}
favFac.getFavorites = function () {
return favorites;
};
return favFac;
}])
.factory('$localStorage', ['$window', function($window) {
return {
store: function(key, value) {
$window.localStorage[key] = value;
},
get: function(key, defaultValue) {
return $window.localStorage[key] || defaultValue;
},
storeObject: function(key, value) {
$window.localStorage[key] = JSON.stringify(value);
},
getObject: function(key,defaultValue) {
return JSON.parse($window.localStorage[key] || defaultValue);
}
//removeItem: function(key){
// $window.localStorage.removeItem(key);
//}
}
controller.js
.filter('favoriteFilter', 'localStorage', function (localStorage) {
if(localStorage.getItem('favorites')!=undefined)
{
var out = [];
return out;
}
else{
return function (dishes) {
var old_favorite = JSON.parse($localStorage.get('favorites'));
var leng = Object.keys(old_favorite).length;
console.log(leng);
var out = [];
for (var i = 0; i < leng; i++) {
for (var j = 0; j < dishes.length; j++) {
if (dishes[j].id === favorites[i].id)
out.push(dishes[j]);
}
}
return out;
}}
});
举个例子,localstorage里面有一个这样的数组
Key : favorites
value : [{"id":1},{"id":2},{"id":0}]
所以,逻辑是,我根据ID和过滤函数比较来自数据库和本地存储的ID
如果ID相同,则数据库中的数据将其推入收藏夹菜单。
但是,它无法显示在收藏夹菜单中,当我在控制台上查看时,它说
[ng:areq] Argument 'fn' is not a function, got string
我在这里做错了吗?还是我放错方法了?
提前致谢。
您提供的错误似乎是语法问题。您缺少数组括号。
.filter('favoriteFilter', ['$localStorage', function (localStorage) {
if(localStorage.getItem('favorites')!=undefined)
{
var out = [];
return out;
}
else
{
return function (dishes) {
var old_favorite = JSON.parse($localStorage.get('favorites'));
var leng = Object.keys(old_favorite).length;
console.log(leng);
var out = [];
for (var i = 0; i < leng; i++) {
for (var j = 0; j < dishes.length; j++) {
if (dishes[j].id === favorites[i].id)
out.push(dishes[j]);
}
}
return out;
}
};
}]);
我没有检查你的逻辑功能,这将是解决你错误的答案。
尝试不同的方法:
由于您已经拥有 addToFavorites 和 deleteFromFavorites 功能,您只需按照以下 3 个步骤操作即可:
定义你的'favorites'数组时,只需按如下方式赋值即可: var 收藏夹 = JSON.parse(window.localStorage['favorites'] || []);
在您的 addToFavorites 函数中,将添加的项目推送到数组后,添加:window.localStorage['favorites'] = JSON.stringify(favorites);
在 deleteFromFavorites 函数中,拼接数组后,添加:window.localStorage['favorites'] = JSON.stringify(favorites);
这三个超级简单的步骤你应该会很好!