We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Adam P • 7 years ago

Haven't tried it yet myself, but is there any reason this can't be used to convert to a "new Date()" object? Kendo datepicker requires a Date object for input, and something like this would be helpful.

Rico Suter • 7 years ago

The conversion logic can be changed, so this should also work with Date objects...

Max Randolph • 8 years ago

Hey, not sure if anyone is still running into this, but you can avoid messing with the property decoration pain by simply wrapping your input variable in brackets. So instead of writing your html element like so: <my-component [enabled]="false"></my-component> Simply use: And Angular2+ should recognize the input as the correct type(boolean). Thanks, Max

Rico Suter • 8 years ago

Yes, of course this is possible. The described technique tries to avoid problems when users miss the brackets or can be used to add transformation logic...

Frank • 9 years ago

Good source. I would appreciate some examples as well. Not sure what is wrong but when I tried to create my own decorator and trying to access the the Object the

this [key] is undefined as well as when I call Object.getOwnPropertyDescriptor(target, key); I get nothing.
not sure what I am doing wrong so some plunkr would be great inspiration.

Rico Suter • 9 years ago

Just tried it with the latest Angular 2 release and it still works for me.

Rico Suter • 9 years ago

Thanks for your comment. As soon as I find the time, I'll add a sample project or a plunkr. Maybe it is not working with the latest Angular 2 release?

Frank • 9 years ago

Not sure I just started to debug it. but simple straigh forward fails. Its so simple that nothing cant go wrong.

export function Test() { 
return function (target: Object, key: string) {
var _val = this[key]; console.log('target: ' + _val);
var ownPropertyDescriptor = Object.getOwnPropertyDescriptor(target, key);
console.log('target2: ' + ownPropertyDescriptor);
}
}
Isidoro Aguilera • 9 years ago

You must put inputs between [] to work properly.

Instead of enabled="false" write [enabled]="false"

Then angular2 sets a boolean value without the need of the custom decorator.

saiprasad • 7 years ago

great it works :)

Akash • 9 years ago

Rico Suter, thank you for this article.

Isidoro Aguilera, thank you for your comment.

Rico Suter • 9 years ago

Thanks for your comment. However, not all users of your component may know this and therefore using the converters is more safe...

Prasad Kumbhare • 10 years ago

Thanks so much.. ran into same issue.. appreciate if you could provide a plunkr or github source code. Many Thanks Prasad