@loopback/docs
Version:
Documentation files rendered at [https://loopback.io](https://loopback.io)
80 lines (44 loc) • 1.86 kB
Markdown
---
lang: en
title: 'API docs: context.binding.toinjectable'
keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI
sidebar: lb4_sidebar
editurl: https://github.com/loopbackio/loopback-next/tree/master/packages/context
permalink: /doc/en/lb4/apidocs.context.binding.toinjectable.html
---
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [@loopback/context](./context.md) > [Binding](./context.binding.md) > [toInjectable](./context.binding.toinjectable.md)
## Binding.toInjectable() method
Bind to a class optionally decorated with `@injectable`<!-- -->. Based on the introspection of the class, it calls `toClass/toProvider/toDynamicValue` internally. The current binding key will be preserved (not being overridden by the key inferred from the class or options).
This is similar to [createBindingFromClass()](./context.createbindingfromclass.md) but applies to an existing binding.
**Signature:**
```typescript
toInjectable(ctor: DynamicValueProviderClass<T> | Constructor<T | Provider<T>>): this;
```
## Parameters
<table><thead><tr><th>
Parameter
</th><th>
Type
</th><th>
Description
</th></tr></thead>
<tbody><tr><td markdown="1">
ctor
</td><td markdown="1">
[DynamicValueProviderClass](./context.dynamicvalueproviderclass.md)<!-- --><T> \| [Constructor](./context.constructor.md)<!-- --><T \| [Provider](./context.provider.md)<!-- --><T>>
</td><td markdown="1">
A class decorated with `@injectable`<!-- -->.
</td></tr>
</tbody></table>
**Returns:**
this
## Example
```ts
@injectable({scope: BindingScope.SINGLETON, tags: {service: 'MyService}})
class MyService {
// ...
}
const ctx = new Context();
ctx.bind('services.MyService').toInjectable(MyService);
```