【发布时间】:2015-05-26 09:02:07
【问题描述】:
我想在单击按钮时使用 angularjs 将值添加到本地存储。我使用 ngStorage 模块
【问题讨论】:
标签: angularjs angular-local-storage ng-storage
我想在单击按钮时使用 angularjs 将值添加到本地存储。我使用 ngStorage 模块
【问题讨论】:
标签: angularjs angular-local-storage ng-storage
首先,将函数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>
【讨论】:
要跳过重复项,需要进行小检查
$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
});
}
}
}
【讨论】: