TypeError: Невозможно установить свойство null при использовании локального хранилища.
Мой код работал хорошо, пока я не ввел локальное хранилище. Что я делаю: когда устройство готово, проверьте, существуют ли значения, и если да, то получите их. В теле функции я присваиваю значение этой области видимости переменной, и именно здесь я получаю эту ошибку.
$ionicPlatform.ready(function() {
if (localStorageService.get("modelTrue") == null) {
$scope.userModal.show();
localStorageService.set("modelTrue", "true")
}
else {
//Parameter to restore:
if(localStorageService.get("first") != null){
$scope.data.firstDisplay = JSON.parse(localStorageService.get("first"));
}
};
$scope.scanBarcode = function() {
$scope.data.firstDisplay = {'src' : 'img/1.png'}; //this is where I get the error
localStorageService.set("first", JSON.stringify($scope.data.firstDisplay));
}
Когда я вызываю последнюю функцию, я получаю сообщение об ошибке: "TypeError: Невозможно установить свойство 'firstDisplay' из null".
Я не понимаю это поведение. Я просто устанавливаю переменную в значение. Что бы это могло значить?
1 ответ
Решение
Вы должны объявить $scope.data ={}
; в противном случае это всегда ноль,
$ionicPlatform.ready(function() {
$scope.data ={};
if (localStorageService.get("modelTrue") == null) {
$scope.userModal.show();
localStorageService.set("modelTrue", "true")
} else {
//Parameter to restore:
if (localStorageService.get("first") != null) {
$scope.data.firstDisplay = JSON.parse(localStorageService.get("first"));
}
};
$scope.scanBarcode = function() {
$scope.data.firstDisplay = {
'src': 'img/1.png'
}; //this is where I get the error
localStorageService.set("first", JSON.stringify($scope.data.firstDisplay));
}