Improve module.filter() by allowing $stateful to be defined.

This commit is contained in:
Oscar Busk 2018-06-28 12:27:49 +02:00
parent 80d50f06cf
commit f6a902f8ce
2 changed files with 22 additions and 6 deletions

View File

@ -214,11 +214,18 @@ mod.factory({
name1(foo: any) {},
name2: ['foo', (foo: any) => {}]
});
mod.filter('name', ($scope: ng.IScope) => {});
mod.filter('name', ['$scope', ($scope: ng.IScope) => {}]);
mod.filter('name', ($scope: ng.IScope) => () => {});
mod.filter('name', ['$scope', ($scope: ng.IScope) => () => {}]);
mod.filter({
name1(foo: any) {},
name2: ['foo', (foo: any) => {}]
name1(foo: any) { return () => {}; },
name2: ['foo', (foo: any) => () => {}],
});
const customStatefulFilter: ng.FilterFunction<(s: string) => number> = (s) => 1;
mod.filter('name', () => customStatefulFilter);
mod.filter('name', ['$scope', () => customStatefulFilter]);
mod.filter({
name1: () => customStatefulFilter,
name2: ['foo', () => customStatefulFilter],
});
mod.provider('name', ($scope: ng.IScope) => ({ $get: () => { } }));
mod.provider('name', TestProvider);

View File

@ -250,8 +250,8 @@ declare namespace angular {
*/
factory(name: string, $getFn: Injectable<Function>): IModule;
factory(object: {[name: string]: Injectable<Function>}): IModule;
filter(name: string, filterFactoryFunction: Injectable<Function>): IModule;
filter(object: {[name: string]: Injectable<Function>}): IModule;
filter(name: string, filterFactoryFunction: Injectable<FilterFactory<Function>>): IModule;
filter(object: {[name: string]: Injectable<FilterFactory<Function>>}): IModule;
provider(name: string, serviceProviderFactory: IServiceProviderFactory): IModule;
provider(name: string, serviceProviderConstructor: IServiceProviderClass): IModule;
provider(name: string, inlineAnnotatedConstructor: any[]): IModule;
@ -2237,4 +2237,13 @@ declare namespace angular {
interface IHttpParamSerializer {
(obj: Object): string;
}
type FilterFunction<Filter extends Function> = {
/**
* By default, filters are only run once the input value changes. By marking the filter as `$stateful`, the filter will be run on every `$digest` to update the output. **This is strongly discouraged.**
* See https://docs.angularjs.org/guide/filter#stateful-filters
*/
$stateful?: boolean
} & Filter;
type FilterFactory<Filter extends Function> = (...I: any[]) => FilterFunction<Filter>;
}