【问题标题】:How to write angular filter definition in controller with typescript如何使用打字稿在控制器中编写角度过滤器定义
【发布时间】:2015-09-14 15:28:08
【问题描述】:

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

我的 html 有带有 ng-repeat 的 div 和一个过滤器定义,该定义在作用域上可用,如下

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

并且视图的控制器类具有范围 vm 属性 searchText、item 和 filterItems(过滤器定义),如下所示

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

我想在 typescript 中转换这个 filterItems,

提前致谢

【问题讨论】:

  • 这已经在 TypeScript 中了,不是吗?你需要改变什么?
  • 我担心“filterItems”方法本身返回一个函数,filterItems方法中是否可以没有函数文本?

标签: angularjs typescript


【解决方案1】:

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

我想在 typescript 中转换这个 filterItems,

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

【讨论】:

  • 我担心“filterItems”方法本身返回一个函数,filterItems方法中是否可以没有函数?
  • 没有。角度过滤器必须是一个函数。 FilterItems是一个函数生成器,生成的函数然后被angular使用。
猜你喜欢
  • 2016-01-20
  • 2015-06-23
  • 2016-04-27
  • 1970-01-01
  • 2016-03-08
  • 2016-10-14
  • 2015-07-19
  • 1970-01-01
  • 2016-12-11
相关资源
最近更新 更多