【发布时间】:2018-07-16 16:56:57
【问题描述】:
我似乎无法使用 AngularJS 和 localstorage 让过滤器为我的项目工作。没有错误,但是当我在输入字段中输入单词时,什么也没有发生。有人愿意帮助我吗? :)
html:
<div ng-app="myApp" ng-controller="ViewSummaryCtrl">
<input type="text" ng-model="query">
</div>
<div class="flex-container" ng-repeat="expense in expenses | filter: {$:query}">
<div style="flex-grow: 2" class="date-col">{{expense.date}} </div>
<div style="flex-grow: 2" class="category-col">{{expense.category}} </div>
<div style="flex-grow: 3" class="description-col"> {{expense.description}}</div>
<div style="flex-grow: 2" class="amount-col">{{expense.amount| currency: "kr"}} </div>
<div style="flex-grow: 1" class="button-col">
<button class="button" type="button" ng-click="edit()">Edit</a>
controller.js
.controller('ViewSummaryCtrl', ['$scope', 'expService', 'categoryList',
function ($scope, expService, categoryList) {
$scope.expenses = expService.getExpense();
Services.js
//this function shows the expenses stored in localStorage
getExpense: function () {
expenses = [];
var prefixLength = prefix.length;
Object.keys(localStorage)
.forEach(function (key) {
if (key.substring(0, prefixLength) == prefix) {
//iterate through each item in localStorage and match it to the prefix key
var item = window.localStorage[key];
//convert stringified data back into JSON format
item = JSON.parse(item);
item.key = key;
expenses.push(item);
}
});
console.log(expenses);
return expenses;
},
【问题讨论】:
标签: javascript angularjs local-storage filtering