0

ローカルストレージを導入するまで、自分のコードは正常に動作しています。私がしているのは、デバイスが準備完了状態になっているかどうかチェックし、値が存在すれば取得します。関数の本体では、変数スコープに値を代入します。これは、このエラーが発生する場所です。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はconsole.log($ scope.data)から得られます。 enter image description here

答えて

1

$scope.data ={}を宣言する必要があります。それ以外の場合は常にnullです。

$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)); 
      } 
+0

何を宣言しますか? $ scope.dataは既にコントローラの中で宣言されています。 This.readyは私のコントローラの中にもあります。 –

+0

あなたは確かですか?$ scope.data.firstDisplayを設定する前にconsole.logを入れて、ここに投稿してください – Sajeetharan

+0

新しい編集を確認してください –

関連する問題