AngularJS过滤器无法正常工作

时间:2015-11-28 13:19:31

标签: javascript angularjs typescript

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

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

请你把我放在正确的方向,我在下面的代码中做错了吗?提前谢谢!

模型

module Models {
    "use strict";

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

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

控制器

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>

1 个答案:

答案 0 :(得分:1)

试试这个:

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

删除HTML

中的过滤器部分