角。JS未捕获的类型错误:不能分配只读属性。

时间:2022-06-13 19:43:37

Trying to create an app, with a toggle setting that should be saved to local storage, haven't gotten much luck with it, though.

不过,在尝试创建一个应用程序时,它的切换设置应该保存到本地存储中,但这并没有带来什么好处。

I'm using Ionic + Angular.JS

我用的是Ionic + angular。js

Here's my code, I'm getting the error Uncaught TypeError: Cannot assign to read only property

这是我的代码,我得到的错误是未捕获的TypeError:不能赋值为只读属性

Controller.js:

Controller.js:

.controller("ExampleController", ['$scope', '$window', function($scope, $window) { 


    $scope.CONFIG = localStorage.getItem('CONFIG');
        if (!$scope.CONFIG) {
          $scope.CONFIG = {karton: true};
        }

        $scope.save = function(checked) {
          $scope.CONFIG.karton = checked;
          localStorage.setItem('CONFIG', $scope.CONFIG);
        }

}]);

the HTML file:

HTML文件:

<div  class="item item-avatar item-icon-right">
       <img src="img/box.jpg"> 
       <ion-toggle type="checkbox" ng-model="karton.checked" ng-change="save(karton.checked)" ng-init="karton.checked = CONFIG.karton" toggle-class="toggle-balanced" id="karton" name="karton">Karton</ion-toggle>
       <div>Box Value: <span class="val">{{karton }}</span></div>
</div>

EDIT

编辑

I now have this but it returns an "undefined" in the console log:

我现在有了这个,但是它在控制台日志中返回一个“未定义的”:

.controller("ExampleController", ['$scope', '$window', function($scope, $window) { 
    $scope.CONFIG = (localStorage.getItem('CONFIG'));
        if (!$scope.CONFIG) {
          $scope.CONFIG = {karton: true};

        }
        $scope.save = function(checked) {
          JSON.stringify($scope.CONFIG.karton) === checked;
          localStorage.setItem('CONFIG', JSON.stringify($scope.CONFIG));
          console.log(JSON.stringify($scope.CONFIG.karton));
        }
}]);

1 个解决方案

#1


2  

The value of $scope.CONFIG is an object, localStorage only supports strings. Use JSON.stringify() and JSON.parse().

美元的价值范围。配置是一个对象,localStorage只支持字符串。使用JSON.stringify()和JSON.parse()。

.controller("ExampleController", ['$scope', '$window', function($scope, $window) { 
    $scope.CONFIG = JSON.parse(localStorage.getItem('CONFIG'));
        if (!$scope.CONFIG) {
          $scope.CONFIG = {karton: true};
        }
        $scope.save = function(checked) {
          $scope.CONFIG.karton = checked;
          localStorage.setItem('CONFIG', JSON.stringify($scope.CONFIG));
        }
}]);

#1


2  

The value of $scope.CONFIG is an object, localStorage only supports strings. Use JSON.stringify() and JSON.parse().

美元的价值范围。配置是一个对象,localStorage只支持字符串。使用JSON.stringify()和JSON.parse()。

.controller("ExampleController", ['$scope', '$window', function($scope, $window) { 
    $scope.CONFIG = JSON.parse(localStorage.getItem('CONFIG'));
        if (!$scope.CONFIG) {
          $scope.CONFIG = {karton: true};
        }
        $scope.save = function(checked) {
          $scope.CONFIG.karton = checked;
          localStorage.setItem('CONFIG', JSON.stringify($scope.CONFIG));
        }
}]);