admin管理员组文章数量:1317898
I have a very simple angular filter.
This filter takes in input a member of an enum (called here XEnum) and returns the string which represent the member in the enum :
module Filters {
"use strict";
export function XEnumToStringFilter() {
return ( input: XEnum ) => {
return XEnum[input];
}
}
}
[...]
module Model {
export enum XEnum{
Started = 0,
Stopped = 1
}
}
[...]
app.filter( "xEnumToStringFilter", [Filters.XEnumToStringFilter] );
This works very well when I use xEnumToStringFilter in my views :
{{0 | etatTransfertEnumToStringFilter}}
print Started
{{1 | etatTransfertEnumToStringFilter}}
print Stopped
But i want to use this filter in my service :
app.service( "serviceUsingXEnum",
["xEnumToStringFilter",
Services.ServiceUsingXEnum] );
But in my service constructor i only get a strange error :
module Services {
"use strict";
export class ServiceUsingXEnum {
constructor(
private xEnumToStringFilter: Filters.XEnumToStringFilter // error here
) {
// some beautiful code ...
}
}
}
Module
Filters
has no exported memberXEnumToStringFilter
Even when my autopletion say it exist !
I want to use dependency injection, i could just do Filters.XEnumToStringFilter()(somethingXEnum)
but that's bad !
Why can't I use XEnumToStringFilter as a type ?
What is a better way to solve it ?
I have a very simple angular filter.
This filter takes in input a member of an enum (called here XEnum) and returns the string which represent the member in the enum :
module Filters {
"use strict";
export function XEnumToStringFilter() {
return ( input: XEnum ) => {
return XEnum[input];
}
}
}
[...]
module Model {
export enum XEnum{
Started = 0,
Stopped = 1
}
}
[...]
app.filter( "xEnumToStringFilter", [Filters.XEnumToStringFilter] );
This works very well when I use xEnumToStringFilter in my views :
{{0 | etatTransfertEnumToStringFilter}}
print Started
{{1 | etatTransfertEnumToStringFilter}}
print Stopped
But i want to use this filter in my service :
app.service( "serviceUsingXEnum",
["xEnumToStringFilter",
Services.ServiceUsingXEnum] );
But in my service constructor i only get a strange error :
module Services {
"use strict";
export class ServiceUsingXEnum {
constructor(
private xEnumToStringFilter: Filters.XEnumToStringFilter // error here
) {
// some beautiful code ...
}
}
}
Module
Filters
has no exported memberXEnumToStringFilter
Even when my autopletion say it exist !
I want to use dependency injection, i could just do Filters.XEnumToStringFilter()(somethingXEnum)
but that's bad !
Why can't I use XEnumToStringFilter as a type ?
What is a better way to solve it ?
Share Improve this question edited Apr 16, 2015 at 15:48 antoinestv asked Apr 16, 2015 at 14:21 antoinestvantoinestv 3,3062 gold badges25 silver badges39 bronze badges2 Answers
Reset to default 4That's because you're using the function as Type Declaration. You either:
1) Change the service's constructor declaration:
constructor(private xEnumToStringFilter: ( enum: XEnum ) => string )
or
2) Create an Interface and use it interface where you want to use the filter:
module Filters {
"use strict";
export interface IXEnumToStringFunction {
( input: XEnum ) => string
}
export function XEnumToStringFilter() {
return ( input: XEnum ) => {
return XEnum[input];
}
}
}
...
then in the constructor
constructor(private xEnumToStringFilter: Filters.IXEnumToStringFunction )
There are two things going on here that I will try to address separately.
1 - A filter is a function not a type
The closest you can get to typing this would be:
private xEnumToStringFilter: () => string
2 - Improper usage of a filter inside service/controller
In order to use a filter in a service/controller you must inject in the $filter
service which you can use to get a reference to your filter via the name it is registered, see the example here
本文标签: javascriptAngular FilterTypescriptStack Overflow
版权声明:本文标题:javascript - Angular Filter + Typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742035942a2417293.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论