简体   繁体   English

AngularJS过滤器无法正常工作

[英]AngularJS filter is not working properly

I have a filter that I would like to use with ng-repeat 我有一个要与ng-repeat一起使用的过滤器

There is no console messages appears on the browser so I'm not be able to see what is going on. 浏览器上没有控制台消息,所以我看不到正在发生什么。

Would you please put me on right direction, where I am doing wrong in the code below? 您是否可以使我朝正确的方向前进,在以下代码中我做错了什么? Thanks in advance! 提前致谢!

Models 楷模

module Models {
    "use strict";

    export interface IMember {
        Id: number;
        // ...
    }

    export interface IForumContent extends IMember {
        RowId: string;
        // ...
    }
}

Controllers 控制器

module Controllers {
    "use strict";

    export class ForumController extends ControllerBase.Controller {
        log: ng.ILogService;
        scope: IForumControllerScope;

        static $inject = ["$scope", "$log"];
        constructor($scope: IForumControllerScope, $log: ng.ILogService) {
            super($scope);
            this.log = $log;
            // ...
        }

        // ...
    }
}

Filters 筛选器

module Filters {
    "use strict";

    export var $inject = ["$log", "$filter"];
    export function UniqueFilter($log: ng.ILogService, $filter: ng.IFilterService) {
        $log.log("Test 1");
        console.log("Test 1");

        return function(items: Array<Models.ForumContent>, filterOn: string) {
            $log.log("Test 2");
            console.log("Test 2");

            if ((filterOn || angular.isUndefined(filterOn)) && angular.isArray(items)) {
                var newItems = [];

                var extractValueToCompare = function(item) {
                    if (angular.isObject(item) && (angular.isString(filterOn))) {
                        return item[filterOn];
                    } else {
                        return item;
                    }
                };

                angular.forEach(items, function(item) {
                    var valueToCheck, isDuplicate = false;

                    for (var i = 0; i < newItems.length; i++) {
                        if (angular.equals(extractValueToCompare(newItems[i]), extractValueToCompare(item))) {
                            isDuplicate = true;
                            break;
                        }
                    }

                    if (!isDuplicate) {
                        newItems.push(item);
                    }

                });
                items = newItems;
            }
            return items;
        }
    }
}

Global 全球

var applicationModule = angular.module("app", []);
applicationModule.filter('uniqueFilter', Filters.UniqueFilter);
applicationModule.controller("forumController", Controllers.ForumController);

Html HTML

<table class="table table-hover">
    <tbody>
        <tr>
            <th>Content</th>
        </tr>
        <tr ng-model="forumContent" ng-repeat="forumContent in forumRepository.Forum.Contents | filter: uniqueFilter:'Id'">
        </tr>
    </tbody>
</table>

Try this: 尝试这个:

<tr ng-model="forumContent" ng-repeat="forumContent in forumRepository.Forum.Contents | uniqueFilter:'Id'"></tr>

Remove filter: part in HTML 删除过滤器: HTML中的一部分

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

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