我是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的localstorage之间的ID与过滤函数进行比较
如果ID相同,那么数据库中的数据会将其推送到收藏夹菜单。
但是,它无法在收藏夹菜单中显示,当我在控制台上查看时,它说
[ng:areq]论据&#39; fn&#39;不是函数,有字符串
我在这里做错了吗?或者我可能在这里放错了方法?
提前谢谢。
答案 0 :(得分:1)
您提出的错误似乎是一个语法问题。你缺少数组括号。
.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;
}
};
}]);
我没有检查你的逻辑功能,这将是解决你的错误的答案。
答案 1 :(得分:0)
尝试不同的方法:
由于您已经有addToFavorites和deleteFromFavorites函数,所以您只需按照以下3个步骤操作:
定义“收藏夹”数组时,只需按如下方式指定: var favorites = JSON.parse(window.localStorage ['favorites'] || []);
在addToFavorites函数中,将添加的项目推送到数组后,添加:window.localStorage ['favorites'] = JSON.stringify(favorites);
在您的deleteFromFavorites函数中,在拼接数组后,添加:window.localStorage ['favorites'] = JSON.stringify(favorites);
你应该选择这三个超级简单的步骤!