<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mthobisi</title>
    <description>The latest articles on DEV Community by Mthobisi (@mthobis41586080).</description>
    <link>https://dev.to/mthobis41586080</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F204610%2Fdeffb03a-43dd-445a-af93-2a58f987a14a.jpg</url>
      <title>DEV Community: Mthobisi</title>
      <link>https://dev.to/mthobis41586080</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mthobis41586080"/>
    <language>en</language>
    <item>
      <title>Toggle This: Feature Flags in Angular</title>
      <dc:creator>Mthobisi</dc:creator>
      <pubDate>Fri, 12 Sep 2025 14:05:54 +0000</pubDate>
      <link>https://dev.to/mthobis41586080/toggle-this-feature-flags-in-angular-34jj</link>
      <guid>https://dev.to/mthobis41586080/toggle-this-feature-flags-in-angular-34jj</guid>
      <description>&lt;h2&gt;
  
  
  Angular Directives
&lt;/h2&gt;

&lt;p&gt;Angular uses directives to add additional behavior to elements. There are several built-in directives that help manage different aspects of your application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Directive types
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Directive Type&lt;/th&gt;
&lt;th&gt;Details&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Components&lt;/td&gt;
&lt;td&gt;Used with a template. This type of directive is the most common directive type.&lt;/td&gt;
&lt;td&gt;Any angular component&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Attributes directives&lt;/td&gt;
&lt;td&gt;Change the appearance or behavior of an element, component, or another directive.&lt;/td&gt;
&lt;td&gt;NgClass NgStyle, NgModel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Structural directives&lt;/td&gt;
&lt;td&gt;Change the DOM layout by adding and removing DOM elements.&lt;/td&gt;
&lt;td&gt;ngIf, ngFor&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Our focus will be on Structural directives, so let us take a deep dive into them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Do We Need Structural Directives?
&lt;/h2&gt;

&lt;p&gt;Structural directives are useful when we want to dynamically change the structure of the HTML Document Object Model (DOM) by adding, removing, or transforming elements based on a condition or data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key reasons for using structural directives
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Conditional rendering &lt;/li&gt;
&lt;li&gt;Dynamic lists&lt;/li&gt;
&lt;li&gt;Managing DOM structure&lt;/li&gt;
&lt;li&gt;Reusability&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Toggle This: What Are Feature Flags?
&lt;/h2&gt;

&lt;p&gt;Feature flags are a software development technique that allows teams to enable or disable specific features in an application at runtime, without changing or redeploying the code.&lt;/p&gt;

&lt;p&gt;In Angular, feature flags are implemented as custom structural directives. They are similar to ngIf, but with key differences.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does the two differ
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Purpose
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;ngIf:&lt;/strong&gt; Used for conditional rendering based on a boolean expression in the code. It primarily controls UI elements based on local component state or simple logic.&lt;br&gt;
&lt;strong&gt;Feature Flags:&lt;/strong&gt; A mechanism to remotely enable or disable features in an application without requiring a new deployment. They help manage features across different environments, support A/B testing, and enable progressive rollouts.&lt;/p&gt;
&lt;h4&gt;
  
  
  Control Mechanism
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;ngIf:&lt;/strong&gt; The condition for *ngIf is determined by a boolean expression directly within your Angular template or component logic. Changes require modifying the code and redeploying the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Feature Flags:&lt;/strong&gt; The state of a feature flag is typically managed externally, often through a dedicated feature flag service or platform. This allows toggling features on or off at runtime without code changes or redeployments.&lt;/p&gt;
&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;To create a directive, you can use the Angular CLI as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng generate directive &amp;lt;directive-name&amp;gt; --standalone
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following class should be generated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Directive({
  selector: '[appFeatureFlag]',
  standalone: true,
})
export class FeatureFlagDirective implements OnInit, OnDestroy {
  @Input('appFeatureFlag') featureFlag!: string;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The @Directive decorator registers this class as a directive. The selector is the directive name (appFeatureFlag). The @Input decorator allows Angular to pass a value when the directive is used. &lt;/p&gt;

&lt;h3&gt;
  
  
  Directive Logic
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private subscription?: Subscription;

  constructor(
    private templateRef: TemplateRef&amp;lt;any&amp;gt;,
    private viewContainer: ViewContainerRef,
    private featureFlagService: FeatureFlagService
  ) {}

  public ngOnInit() {
    if (!this.featureFlag) {
      this.viewContainer.clear();
      return;
    }

    this.subscription = this.featureFlagService
      .hasAccess(this.featureFlag)
      .subscribe((hasAccess) =&amp;gt; {
        this.viewContainer.clear();
        if (hasAccess) {
          this.viewContainer.createEmbeddedView(this.templateRef);
        }
      });
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation: On initialization, we clear the viewContainer, which effectively removes a component or element from the DOM. Then, if a feature flag is provided, we check access and render the component only if access is granted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deep Dive To a Feature Flag Service
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private checkFullPath(nodes: ItemNames[] | undefined, segments: string[]): boolean {
    if (!nodes) {
      return false;
    }

    const [current, ...rest] = segments;

    for (const node of nodes) {
      if (node.name === current) {
        // Recurse into children for the remaining segments
        if (rest.length === 0) {
          return true; // Last segment matched
        }

        if (node.itemNames &amp;amp;&amp;amp; node.itemNames.length &amp;gt; 0) {
          return this.checkFullPath(node.itemNames, rest);
        } else {
          return false; // No children to match remaining segments
        }
      }
    }

    return false; // Current segment not found
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation: Due to the hierarchical structure of features, we split the feature flag into segments and recursively check each parent and its children to confirm if a feature is accessible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pros and Cons of feature flags
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Pros
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Dynamic Control: Features can be turned on or off without redeploying code.&lt;/li&gt;
&lt;li&gt;Safe Rollouts: Supports progressive feature rollouts and A/B testing.&lt;/li&gt;
&lt;li&gt;Environment Flexibility: Different features can be enabled for different environments or user groups.&lt;/li&gt;
&lt;li&gt;Reduced Risk: Bugs in new features can be quickly disabled without impacting the whole system.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cons
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Increased Complexity: Managing many flags can make the codebase harder to maintain.&lt;/li&gt;
&lt;li&gt;Technical Debt: Old or unused flags need to be cleaned up to prevent clutter.&lt;/li&gt;
&lt;li&gt;Testing Challenges: Testing all combinations of feature flags can be complex.&lt;/li&gt;
&lt;li&gt;Performance Overhead: Frequent checks on feature flags might introduce minimal runtime overhead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a fronend code: &lt;a href="https://github.com/mthobisig8/feature-flag-fe" rel="noopener noreferrer"&gt;https://github.com/mthobisig8/feature-flag-fe&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
