WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Angular · Advanced · question 57 of 100

How do you create and manage feature flags in Angular applications?

📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

Feature flags (or feature toggles) are a software development technique that allows you to enable or disable specific features in your application at runtime, without changing the code. This can be useful for testing new features, gradually rolling out changes, or enabling certain features only for specific users or environments.

In an Angular application, you can create and manage feature flags using a combination of services, components, and environment configuration. Here’s a step-by-step guide on how to implement it.

**Step 1: Define your feature flags**

Create a ‘feature-flags.ts‘ file to define your feature flags. You can use an interface to represent the structure of the feature flags. For example:

// feature-flags.ts
export interface FeatureFlags {
  newDashboard: boolean;
  advancedSearch: boolean;
}

**Step 2: Create a FeatureFlagService**

Create a ‘feature-flag.service.ts‘ file to manage the feature flags. This service will load the feature flags from an external source or use the flags provided via the environment configuration.

// feature-flag.service.ts
import { Injectable } from '@angular/core';
import { FeatureFlags } from './feature-flags';

@Injectable({
  providedIn: 'root',
})
export class FeatureFlagService {
  private featureFlags: FeatureFlags;

  constructor() {
    // Load feature flags from environment or any other source
    this.featureFlags = {
      newDashboard: true,
      advancedSearch: false,
    };
  }

  isEnabled(feature: keyof FeatureFlags): boolean {
    return this.featureFlags[feature];
  }
}

**Step 3: Configure environment-specific feature flags**

In the ‘src/environments‘ folder, configure the feature flags for each environment.

// environment.ts
export const environment = {
  production: false,
  featureFlags: {
    newDashboard: true,
    advancedSearch: false,
  },
};

// environment.prod.ts
export const environment = {
  production: true,
  featureFlags: {
    newDashboard: true,
    advancedSearch: true,
  },
};

Update the ‘FeatureFlagService‘ to load feature flags from the environment.

// feature-flag.service.ts
import { Injectable } from '@angular/core';
import { environment } from '../environments/environment';
import { FeatureFlags } from './feature-flags';

@Injectable({
  providedIn: 'root',
})
export class FeatureFlagService {
  private featureFlags: FeatureFlags = environment.featureFlags;

  isEnabled(feature: keyof FeatureFlags): boolean {
    return this.featureFlags[feature];
  }
}

**Step 4: Use feature flags in components**

Now that you have your FeatureFlagService and environment configurations, you can use these feature flags to hide or show elements in your components. For example:

// app.component.ts
import { Component } from '@angular/core';
import { FeatureFlagService } from './feature-flag.service';

@Component({
  selector: 'app-root',
  template: `
    <div *ngIf="featureFlagService.isEnabled('newDashboard')">
      <app-new-dashboard></app-new-dashboard>
    </div>
    <div *ngIf="!featureFlagService.isEnabled('newDashboard')">
      <app-old-dashboard></app-old-dashboard>
    </div>
  `,
})
export class AppComponent {
  constructor(public featureFlagService: FeatureFlagService) {}
}

In this example, we use the ‘isEnabled()‘ method of the ‘FeatureFlagService‘ to conditionally render the new and old dashboard components based on the ‘newDashboard‘ feature flag.

With this approach, you can create and manage feature flags in Angular applications, and use them to toggle specific features at runtime without changing the code.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Angular interview — then scores it.
📞 Practice Angular — free 15 min
📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

All 100 Angular questions · All topics