Angularjs TypeError:使用本地存储时无法设置null属性
在引入本地存储之前,我的代码一直运行良好。我要做的是:当设备准备就绪时,检查这些值是否存在,如果存在,获取它们。在函数体中,我给变量范围赋值,这就是我得到这个错误的地方Angularjs TypeError:使用本地存储时无法设置null属性,angularjs,ionic-framework,local-storage,angular-local-storage,Angularjs,Ionic Framework,Local Storage,Angular Local Storage,在引入本地存储之前,我的代码一直运行良好。我要做的是:当设备准备就绪时,检查这些值是否存在,如果存在,获取它们。在函数体中,我给变量范围赋值,这就是我得到这个错误的地方 $ionicPlatform.ready(function() { if (localStorageService.get("modelTrue") == null) { $scope.userModal.show(); localStorageService.set("modelTrue", "true") } els
$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”
我不理解这种行为。我只是将一个变量设置为一个值。
这意味着什么
空值来自console.log($scope.data);
您需要声明
$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));
}
您需要声明
$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));
}
申报什么$scope.data已在我的控制器中声明。This.ready也在我的控制器中。您确定吗,在设置$scope.data.firstDisplay和post之前,是否可以放置一个console.log此处查看它是否为空,这是问题的本质,它为空。但我不明白,它以前工作得很好,就在我添加本地存储时,它出现了这个错误。我在控制器开头定义的$scope.data是否足够?它足够了,可能您在代码的某些部分中设置为空声明了什么$scope.data已在我的控制器中声明。This.ready也在我的控制器中。您确定吗,在设置$scope.data.firstDisplay和post之前,是否可以放置一个console.log此处查看它是否为空,这是问题的本质,它为空。但我不明白,它以前工作得很好,就在我添加本地存储时,它出现了这个错误。我在控制器开头定义的$scope.data是否足够?它足够了,可能在代码的某些部分设置为空