|
| 1 | +--- |
| 2 | +slug: 'announcing-angular-sdk' |
| 3 | +title: 'Announcing the OpenFeature Angular SDK!' |
| 4 | +date: 2024-09-17 |
| 5 | +authors: [lukasreining, toddbaert] |
| 6 | +image: /img/blog/2024-09-17-announcing-angular-sdk/angular.png |
| 7 | +description: "Announcing the first official release of the OpenFeature Angular SDK!" |
| 8 | +tags: [angular, ng, sdk, ts, js, javascript, web] |
| 9 | +draft: false |
| 10 | +--- |
| 11 | + |
| 12 | +We are excited to unveil the first official release of the [@openfeature/angular-sdk](/docs/reference/technologies/client/web/angular)! 🚀 |
| 13 | +This SDK extends OpenFeature capabilities to Angular applications, with a focus on Angular's unique patterns and practices. |
| 14 | +In this post, we’ll walk you through some of the standout features and how they integrate seamlessly with Angular development. |
| 15 | + |
| 16 | +<!--truncate--> |
| 17 | + |
| 18 | +<img src={require('@site/static/img/blog/2024-09-17-announcing-angular-sdk/angular.png').default} /> |
| 19 | + |
| 20 | +## Features |
| 21 | + |
| 22 | +### Angular Module System Integration |
| 23 | + |
| 24 | +The Angular SDK exports a module that allows you to configure your [OpenFeature provider](https://openfeature.dev/docs/reference/concepts/provider) using Angular constructs. |
| 25 | +You can set your default provider, and optionally configure additional providers for other domains. |
| 26 | +Besides configuring your provider, you'll also need to use the `OpenFeatureModule` in order to utilize the other features of the SDK. |
| 27 | + |
| 28 | +```ts |
| 29 | +import { NgModule } from '@angular/core'; |
| 30 | +import { CommonModule } from '@angular/common'; |
| 31 | +import { OpenFeatureModule } from '@openfeature/angular-sdk'; |
| 32 | + |
| 33 | +@NgModule({ |
| 34 | + declarations: [ |
| 35 | + // Other components |
| 36 | + ], |
| 37 | + imports: [ |
| 38 | + CommonModule, |
| 39 | + OpenFeatureModule.forRoot({ |
| 40 | + provider: new YourOpenFeatureProvider(), |
| 41 | + // domainBoundProviders are optional, mostly needed if more than one provider is needed |
| 42 | + domainBoundProviders: { |
| 43 | + domain1: new YourOtherOpenFeatureProvider(), |
| 44 | + domain2: new YourOtherOtherOpenFeatureProvider(), |
| 45 | + }, |
| 46 | + }) |
| 47 | + ], |
| 48 | +}) |
| 49 | +export class AppModule { |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Easy Templating with Directives |
| 54 | + |
| 55 | +Perhaps the most useful part of the Angular SDK is the inclusion of the built in evaluation directives. |
| 56 | +These directives handle all the flag evaluation boilerplate for you, using Angular's directive abstractions which add functionality to elements. |
| 57 | +In its most basic form, the directive differentially renders templates based on the flag evaluation result: |
| 58 | + |
| 59 | +```html |
| 60 | +<!-- evaluate a flag called new-message and show a render a different template based on the result --> |
| 61 | +<div |
| 62 | + *booleanFeatureFlag="'new-message'; default: true; else: booleanFeatureElse;"> |
| 63 | + <p>Welcome to this OpenFeature-enabled Angular app!</p> |
| 64 | +</div> |
| 65 | +<ng-template #booleanFeatureElse> |
| 66 | + <p>Welcome to this Angular app.</p> |
| 67 | +</ng-template> |
| 68 | +``` |
| 69 | + |
| 70 | +String, Numeric, and Object flags are also supported. |
| 71 | +For these, you can supply templates based by matching against the returned flag value: |
| 72 | + |
| 73 | +```html |
| 74 | +<!-- render this template if the value == 10 --> |
| 75 | +<div |
| 76 | + *numberFeatureFlag="'discountRate'; value: 10; default: 5; else: numberFeatureElse"> |
| 77 | + This is shown when the feature flag matches the specified discount rate. |
| 78 | +</div> |
| 79 | +<!-- render this template if the value != 10 --> |
| 80 | +<ng-template #numberFeatureElse> |
| 81 | + This is shown when the feature flag does not match the specified discount rate. |
| 82 | +</ng-template> |
| 83 | +``` |
| 84 | + |
| 85 | +Additionally, it's possible to bind the evaluation result and related metadata to the template by omitting the `value` to match: |
| 86 | + |
| 87 | +```html |
| 88 | +<!-- omitting the `value` to match against unconditionally renders this template --> |
| 89 | +<div *stringFeatureFlag="'themeColor'; default: 'light'; let value;"> |
| 90 | + The theme color is {{ value }}. |
| 91 | +</div> |
| 92 | +``` |
| 93 | + |
| 94 | +### Loading and Automatic Re-Rendering |
| 95 | + |
| 96 | +You may want to hide some content until your provider is ready. |
| 97 | +For this reason, the directives support specifying templates to render before your provider is ready, and while it's [reconciling its state](https://openfeature.dev/docs/reference/concepts/sdk-paradigms#static-context-paradigms-client-side-sdks) after context changes. |
| 98 | +The directive will automatically re-render when the provider's readiness changes, or if the flag values themselves change. |
| 99 | + |
| 100 | +```html |
| 101 | +<!-- render different templates based on the readiness of the flag provider --> |
| 102 | +<div |
| 103 | + *booleanFeatureFlag="'my-feature'; default: true; else: booleanFeatureElse; initializing: booleanFeatureInitializing; reconciling: booleanFeatureReconciling"> |
| 104 | + <p>This is shown when the feature flag is enabled.</p> |
| 105 | +</div> |
| 106 | +<ng-template #booleanFeatureElse> |
| 107 | + <p>This is shown when the feature flag is disabled.</p> |
| 108 | +</ng-template> |
| 109 | +<ng-template #booleanFeatureInitializing> |
| 110 | + <p>This is shown when the feature flag is initializing.</p> |
| 111 | +</ng-template> |
| 112 | +<ng-template #booleanFeatureReconciling> |
| 113 | + <p>This is shown when the feature flag is reconciling.</p> |
| 114 | +</ng-template> |
| 115 | +``` |
| 116 | + |
| 117 | +## Compatibility |
| 118 | + |
| 119 | +As with other framework-specific SDKs, the Angular SDK is built on top of the existing `@openfeature/web-sdk`. |
| 120 | +Any OpenFeature provider built to support the web SDK can also be used with the `@openfeature/angular-sdk`, no changes required. |
| 121 | + |
| 122 | +## Looking Ahead |
| 123 | + |
| 124 | +We’re eager to expand the Angular SDK’s capabilities with future updates! |
| 125 | +If you're an Angular developer or interested in exploring how feature flags can enhance your applications, we encourage you to try out the OpenFeature Angular SDK. |
| 126 | +Happy coding! 🎉 |
0 commit comments