javascript - AngularJS 过滤器无法正常工作

标签 javascript angularjs typescript

我有一个过滤器,我想与 ng-repeat 一起使用

浏览器上没有出现控制台消息,因此我无法看到发生了什么。

请您告诉我正确的方向,我在下面的代码中做错了什么?提前致谢!

型号

module Models {
    "use strict";

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

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

Controller

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;
            // ...
        }

        // ...
    }
}

过滤器

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;
        }
    }
}

全局

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

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>

最佳答案

试试这个:

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

Remove filter: part in HTML

关于javascript - AngularJS 过滤器无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33971783/

相关文章:

javascript - Angular 通过另一个组件获取新范围

typescript - 使用嵌套类型参数而不在 TS 中声明两次

javascript - 如何使用 react typescript 输入 map 方法?

javascript - Chart.js:图表未在 iframe 中调整大小

javascript - 如何在 Angular 中的自定义指令内设置 d3 图表?

javascript - 在原型(prototype)方法的回调函数中调用原型(prototype)方法

javascript - Angular 中嵌套订阅的错误

javascript - 使用 jQuery 删除类的正确方法

javascript - 如何将一条消息中包含的 JSON 对象数组转换为多个 JSON 对象消息?

javascript - 为什么 ngInfiniteScroll(无限滚动)不适用于移动设备?