How to write angular filter definition in controller with typescript

时间:2015-09-14 15:28:09

标签: angularjs typescript

I have following working code, its just that i am using javascript, want to convert it into full typescript code.

My html has div with ng-repeat with a filter definition, the definition is available on scope, as follows

<input type="text" ng-model="vm.searchText"/>
<div ng-repeat="item in vm.items | filter:vm.filterItems(vm.searchText)">{{item.name}}</div>

and the view's controller class has scope vm properties searchText, items and a filterItems(filter definition) as follows

export interface IMyScope extends ng.IScope
{
    vm: MyController;
}

export class ItemClass {
    private _name: string;
    constructor(name: string) {
        this._name = name;
    }

    public get name(): string {
        return this._name;
    }
}

export class MyController
{
    private _searchText: string;
    private _items: ItemClass[];
    constructor(scope: IMyScope) {
        scope.vm = this;
        this._items = [];

        this._items.push(new ItemClass("test1"));
        this._items.push(new ItemClass("value2"));
        this._items.push(new ItemClass("foo"));
    }

    public get searchText(): string {
        return this._searchText;
    }

    public set searchText(value: string) {
        this._searchText = value;
    }

    public get items(): ItemClass[] {
        return this._items;
    }

    public filterItems(criteriaText: string) {
        return function (item: ItemClass): boolean {
            if (!criteriaText) {
                return true;
            }

            return item.name.toLowerCase().indexOf(criteriaText.toLowerCase()) > -1;
        }
    }
}

I want to convert this filterItems in typescript,

Thanks in Advance

1 个答案:

答案 0 :(得分:1)

  

只是我正在使用javascript,想将其转换为完整的打字稿代码。

     

我想在typescript中转换这个filterItem,

你拥有的已经是TypeScript 。您可以通过使用类型注释来判断。