简体   繁体   English

使用angularJS从本地存储中过滤数据

[英]filter data from localstorage with angularJS

I can't seem to get filter to work for my project using AngularJS and localstorage. 我似乎无法使用AngularJS和localstorage使过滤器适合我的项目。 There is no error but when I enter words into the input field, nothing happens. 没有错误,但是当我在输入字段中输入单词时,什么也没有发生。 Is anyone willing to help me out? 有人愿意帮助我吗? :) :)

html: 的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.js

.controller('ViewSummaryCtrl', ['$scope', 'expService', 'categoryList',
    function ($scope, expService, categoryList) {           
        $scope.expenses = expService.getExpense();   

Services.js 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;
        },                                                  

$scope.expenses worked in ViewSummaryCtrl, but div.flex-container is not div[ng-app]'s child node. $scope.expenses$scope.expenses工作,但div.flex-container不是div [ng-app]的子节点。 So there is no expenses . 因此没有任何expenses

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM