Ionic:为什么我的基于 $localStorage 的对象数组在设备上未定义?
Ionic: Why is my $localStorage based object array undefined on device?
我在服务中有一个基于 $localStorage 的对象数组:
.service('storedService', function($localStorage){
this.$storage = $localStorage;
this.$storage.storedItems;
this.addToList = function(item){
this.$storage.storedItems.push(item);
};
})
对象数组在控制器中使用:
.controller('storedController', function($scope, $ionicPopup, $http, $localStorage, itemListService, storedService) {
$scope.storedItems = storedService.$storage.storedItems;
$scope.showPopup = function() {
var i = 0;
//Pictures array stores 10 hits with URL of images
$scope.pictures = [];
//newitem is the new object pushed to items array
$scope.newitem = {}
$scope.getPic = function(name) {
$http.get('https://www.googleapis.com/customsearch/v1?key=AIzaSyDKzJO_2-a82Jrn0sA2oSnDgHORcJegCAA&cx=011061616694035020478:dvpxju__yje&searchType=image&lr=lang_no&q=' + name).then(function(resp) {
console.log('Success', resp.data.items);
$scope.pictures = resp.data.items;
$scope.newitem.pic = resp.data.items[0].image.thumbnailLink;
})
$scope.changePic = function() {
if (i == 10) {
i = 0;
} else {
i++;
}
$scope.newitem.pic = $scope.pictures[i].image.thumbnailLink;
}
}
var myPopup = $ionicPopup.show({
template: '<form ng-submit="getPic(newitem.name)"><input type="text" placeholder="Item name" ng-model="newitem.name" ng-blur="getPic(newitem.name)"></br><img alt="Press me!" src="{{newitem.pic}}" style="display:block;width:100px;height:100px;margin-left:auto;margin-right:auto;" ng-click="changePic()"></form>',
title: 'Add new item to list',
subTitle: 'Tip:Tap the image to change it',
scope: $scope,
buttons: [{
text: 'Cancel'
}, {
text: '<b>Save</b>',
type: 'button-positive',
onTap: function(e) {
storedService.addToList($scope.newitem);
}
}]
});
}
$scope.data = {
showDelete: false
};
$scope.moveToItems = function(item){
itemListService.addToList(item);
};
})
这需要一个对象并将其存储在对象数组中。在浏览器中一切正常,但在 Ionic View 和设备上它不起作用。它显示为未定义。为什么它在浏览器中不是未定义的?我做错了什么?
可能设备比您的浏览器慢。尝试将您的代码包装在 $ionicPlatform.ready(function () { //**code*/})
中
你的问题是,如果 localStorage 中当前没有存储任何内容,则 storedItems
不是数组并且未定义
当服务初始化并分配 $storage 时,检查是否定义了 storedItems
,如果未定义,则为其分配一个空数组
this.$storage = $localStorage;
if(!this.$storage.storedItems){
this.$storage.storedItems =[];
}
我在服务中有一个基于 $localStorage 的对象数组:
.service('storedService', function($localStorage){
this.$storage = $localStorage;
this.$storage.storedItems;
this.addToList = function(item){
this.$storage.storedItems.push(item);
};
})
对象数组在控制器中使用:
.controller('storedController', function($scope, $ionicPopup, $http, $localStorage, itemListService, storedService) {
$scope.storedItems = storedService.$storage.storedItems;
$scope.showPopup = function() {
var i = 0;
//Pictures array stores 10 hits with URL of images
$scope.pictures = [];
//newitem is the new object pushed to items array
$scope.newitem = {}
$scope.getPic = function(name) {
$http.get('https://www.googleapis.com/customsearch/v1?key=AIzaSyDKzJO_2-a82Jrn0sA2oSnDgHORcJegCAA&cx=011061616694035020478:dvpxju__yje&searchType=image&lr=lang_no&q=' + name).then(function(resp) {
console.log('Success', resp.data.items);
$scope.pictures = resp.data.items;
$scope.newitem.pic = resp.data.items[0].image.thumbnailLink;
})
$scope.changePic = function() {
if (i == 10) {
i = 0;
} else {
i++;
}
$scope.newitem.pic = $scope.pictures[i].image.thumbnailLink;
}
}
var myPopup = $ionicPopup.show({
template: '<form ng-submit="getPic(newitem.name)"><input type="text" placeholder="Item name" ng-model="newitem.name" ng-blur="getPic(newitem.name)"></br><img alt="Press me!" src="{{newitem.pic}}" style="display:block;width:100px;height:100px;margin-left:auto;margin-right:auto;" ng-click="changePic()"></form>',
title: 'Add new item to list',
subTitle: 'Tip:Tap the image to change it',
scope: $scope,
buttons: [{
text: 'Cancel'
}, {
text: '<b>Save</b>',
type: 'button-positive',
onTap: function(e) {
storedService.addToList($scope.newitem);
}
}]
});
}
$scope.data = {
showDelete: false
};
$scope.moveToItems = function(item){
itemListService.addToList(item);
};
})
这需要一个对象并将其存储在对象数组中。在浏览器中一切正常,但在 Ionic View 和设备上它不起作用。它显示为未定义。为什么它在浏览器中不是未定义的?我做错了什么?
可能设备比您的浏览器慢。尝试将您的代码包装在 $ionicPlatform.ready(function () { //**code*/})
你的问题是,如果 localStorage 中当前没有存储任何内容,则 storedItems
不是数组并且未定义
当服务初始化并分配 $storage 时,检查是否定义了 storedItems
,如果未定义,则为其分配一个空数组
this.$storage = $localStorage;
if(!this.$storage.storedItems){
this.$storage.storedItems =[];
}