【问题标题】:I want to add values to local storage on button click using angularjs我想使用 angularjs 在按钮单击时将值添加到本地存储
【发布时间】:2015-05-26 09:02:07
【问题描述】:

我想在单击按钮时使用 angularjs 将值添加到本地存储。我使用 ngStorage 模块

Demo for plunker

【问题讨论】:

    标签: angularjs angular-local-storage ng-storage


    【解决方案1】:

    首先,将函数cloneItem改为接受类似参数

    $scope.cloneItem = function (todo) {
        $scope.$storage.notes.push({
            "age": todo.age, "id":todo.id
        });
    }
    

    然后在你的视图中,传入相关的todo元素

    <td><button data-ng-click="cloneItem(todo)">insert id and age to localstorage</button></td>
    

    【讨论】:

      【解决方案2】:

      要跳过重复项,需要进行小检查

      $scope.cloneItem = function (todo) {
      for (var i = 0; i < $scope.$storage.notes.length - 1; i++) {
          if ($scope.$storage.notes[i].age !=todo.age && $scope.$storage.notes[i].id!=todo.id ) {
          $scope.$storage.notes.push({
              "age": todo.age, "id":todo.id
          });
         }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多