【问题标题】:filter data from localstorage with angularJS使用 angularJS 从本地存储中过滤数据
【发布时间】: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


    【解决方案1】:

    $scope.expenses 在 ViewSummaryCtrl 中工作,但 div.flex-container 不是 div[ng-app] 的子节点。所以没有expenses

    【讨论】:

    • 哇!你是救生员!!非常感谢:D
    猜你喜欢
    • 2017-07-01
    • 2013-08-17
    • 2016-06-06
    • 2015-03-22
    • 2015-12-05
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    相关资源
    最近更新 更多