Feature flags in Angular with dif's core SDK
dif has no official Angular adapter. To add feature flags in Angular, install the framework-agnostic @dif.sh/sdk directly and wire it into one injectable service. Call dif.init() once at bootstrap, then call dif(id, branches) at each render site. That’s the entire integration, because the SDK is a plain TypeScript function with no proprietary rendering model to plug into.
@dif.sh/react and @dif.sh/svelte exist because those frameworks need a provider and a store. Angular’s dependency injection does the same job with less code: a root-provided service, an ApplicationConfig initializer, and the bare SDK calls. No package to install beyond the SDK itself, no version matrix to track against Angular’s release cadence.
This guide covers installing the core SDK, initializing it at bootstrap, gating a component, and running an A/B test with dif.track(). The last section handles server-side rendering in Angular with the pure assign() function.
Key Takeaways
- dif publishes adapters for React and Svelte only. Angular integration uses
@dif.sh/sdkdirectly, wrapped in one injectable service, about 20 lines of code.dif.init()configures a module-level singleton once. Call it from aprovideAppInitializer()inapp.config.ts, before any component reads a flag.dif(id, branches)returns a thunk. Store it on a component field; invoking it in the template is what triggers bucketing and fires the one exposure event per(experiment, user).- Assignment is a deterministic hash of the user id and the flag’s salt, so there’s no network call and no flicker between Angular’s server-rendered markup and the hydrated client.
- For server-side rendering, Angular has no
difadapter either, so request-scoped assignment uses the same pureassign()escape hatch that React’s adapter falls back to.
Install the core SDK
Install @dif.sh/sdk on its own. There’s no @dif.sh/angular to add alongside it:
npm install @dif.sh/sdk
dif build compiles your active flag files into dif/generated/client.ts, which registers every experiment with the SDK as a side effect of importing it. Import it once, at your app’s entry point, so the registry is populated before anything calls dif():
// main.ts
import "./dif/generated/client";
import { bootstrapApplication } from "@angular/platform-browser";
import { AppComponent } from "./app/app.component";
import { appConfig } from "./app/app.config";
bootstrapApplication(AppComponent, appConfig);
Skip this import and an unknown flag id still resolves safely to its first branch, but it never buckets a user or fires an exposure. dif validate catches a missing registration at build time; this import is what makes the registration exist at runtime.
Initialize dif once, at Angular bootstrap
dif.init() configures a singleton: who the current user is, what attributes they carry, and where exposure events go. Call it exactly once, before the app renders. Angular’s provideAppInitializer() is built for this:
// app.config.ts
import { ApplicationConfig, provideAppInitializer, inject } from "@angular/core";
import { dif } from "@dif.sh/sdk";
import { attributes } from "../dif/generated/audiences";
import { AuthService } from "./auth.service";
export const appConfig: ApplicationConfig = {
providers: [
provideAppInitializer(() => {
const auth = inject(AuthService);
dif.init({
project: "acme-shop",
userId: () => auth.currentUser()?.id ?? null,
attributes: () => attributes({ plan: auth.currentUser()?.plan ?? null }),
});
}),
],
};
The userId resolver is what makes assignment deterministic: the same id always hashes to the same bucket, so there’s nothing to coordinate across components. inject() inside the initializer works the same way it does in any Angular dependency injection context. Add a publishableKey to ship exposure and outcome events to dif Cloud; assignment itself needs none of that, because it’s computed locally.
Gate a component
A flag is just the dif() call. Store the returned thunk on a field and invoke it where the component renders:
import { Component } from "@angular/core";
import { dif } from "@dif.sh/sdk";
@Component({
selector: "app-checkout",
standalone: true,
imports: [NewCheckoutComponent, LegacyCheckoutComponent],
template: `
@if (isNewCheckout()) {
<app-new-checkout />
} @else {
<app-legacy-checkout />
}
`,
})
export class CheckoutComponent {
isNewCheckout = dif("new-checkout", {
off: () => false,
on: () => true,
});
}
isNewCheckout() in the template is the render site. Calling it resolves the bucket and fires exactly one exposure for this (experiment, user) pair, no matter how many times Angular’s change detection re-renders the component. There’s no service method to write beyond this, because dif() is already the method.
Run an A/B test and track the outcome
A flag and an experiment are the same call, structurally. Add a second branch and dif returns the one this user gets:
import { Component } from "@angular/core";
import { dif } from "@dif.sh/sdk";
@Component({
selector: "app-checkout-cta",
standalone: true,
template: `<button (click)="onPlaceOrder()">{{ label() }}</button>`,
})
export class CheckoutCtaComponent {
label = dif("checkout-cta-v2", {
control: () => "Place order",
variant_a: () => "Get it today",
});
onPlaceOrder() {
dif.track("completed_checkout", { value: 49 });
}
}
dif.track() fires the metric that closes the loop, at the point the outcome actually happens, not at render time. When the test concludes and variant_a wins, you ramp it by editing the weights in the flag’s .md file. This component doesn’t change. The A/B testing for developers guide covers the rest of the experiment lifecycle.
Server-side rendering without a flicker
Angular’s server-side rendering renders a page on the server first, then hydrates it in the browser, the same two-render problem React and Svelte have. dif has no Angular SSR adapter either, so the fix is the same pure function React’s own adapter falls back to: assign().
// a server route handler or resolver
import "./dif/generated/client";
import { assign } from "@dif.sh/sdk";
const assignment = assign("checkout-cta-v2", {
userId: req.session.userId,
attributes: { locale: req.headers["accept-language"], plan: req.session.plan },
overrides: {},
});
assign() reads the registry without touching the browser singleton, so it’s safe to call from a long-lived server process. It returns null for an unknown id and otherwise { variant, bucket, exposed, forced }, with no exposure fired.
You don’t need to pass that assignment to the client by hand. Because bucketing is a pure function of the user id and the flag’s salt, resolving the same userId in the browser’s dif.init() config produces the identical variant. The server-rendered markup and the hydrated component agree without any handoff code, and the client’s own dif() call is what fires the one authoritative exposure.
Why there’s no Angular adapter yet
The gap is real: React and Svelte developers get useDif() and experiment() out of the box; Angular developers write the ~20-line service above themselves. That’s the cost of being on the framework dif hasn’t built a wrapper for.
What it buys you is small, because the wrapper was never doing much. @dif.sh/react’s useDif() returns { track, exposure }, where exposure has the same signature as the bare dif(). Angular’s dependency injection, provided services, and the new control-flow syntax (@if, @for) already do the job a provider and a hook exist to do in React. There’s less to abstract, so there’s less missing.
Getting started
Install the SDK, scaffold your flags, and wire the initializer:
npm install -g @dif.sh/cli
dif init
npm install @dif.sh/sdk
dif init writes the dif/ directory and a starter flag; dif build compiles it into the generated client this guide imports. The feature flags page walks the full workflow, and what feature flags are is the primer if you’re new to the concept.
Feature flags in Angular come down to one provideAppInitializer() call and the bare dif() function at each render site. There’s no adapter package, no provider component, and nothing to debug beyond the service you wrote yourself. The flag still lives in your repo, compiles to a typed client, and resolves locally, the same as it does for React and Svelte.
FAQ
Does dif have an Angular SDK? No. dif ships adapters for React (@dif.sh/react) and Svelte (@dif.sh/svelte) only. Angular uses the framework-agnostic @dif.sh/sdk directly; it’s plain TypeScript with zero dependencies, so there’s no adapter layer missing in practice.
How do I add feature flags to an Angular app? Install @dif.sh/sdk, call dif.init() once from a provideAppInitializer() in app.config.ts, and call dif(id, branches) at each component’s render site. That’s the whole integration.
Can I use feature flags with Angular signals? Yes. dif(id, branches) returns a thunk you can call directly from a template or wrap in a computed() signal if you need the value elsewhere in the component. The SDK has no opinion on how Angular renders it.
Does dif work with Angular’s server-side rendering? Yes, through the pure assign() function, which does request-scoped assignment without touching the browser singleton or firing an exposure. Because bucketing is deterministic, the client’s own dif() call lands on the same variant, so the server-rendered and hydrated markup agree.
How big is the dif SDK in an Angular bundle? About 5 kB gzipped, with zero dependencies. There’s no evaluation service to call, because the variant is a hash of the user id computed locally, so Angular pays no extra network round-trip to read a flag.