Magnum23
Magnum23

Reputation: 417

How to write angular filter definition in controller with 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

Upvotes: 0

Views: 470

Answers (1)

basarat
basarat

Reputation: 275997

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

I want to convert this filterItems in typescript,

What you have is already TypeScript. You can tell by the use of type annotations.

Upvotes: 1

Related Questions