【发布时间】: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