Zum Inhalt der Dokumentation springen

Angular

Add the CookieBeam loader to src/index.html in an Angular app and read consent with a signal-based service.

Zuletzt aktualisiert:

In Angular the loader goes in src/index.html, the page every route is served from, including server-rendered pages. Read Install in a web framework first for the rules every framework shares.

Add the loader

Put the loader first in <head>:

<!-- src/index.html -->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>My app</title>
    <base href="/" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- 1. CookieBeam first, synchronous -->
    <script src="https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js" data-cfasync="false" data-no-optimize="1" data-no-defer="1" data-no-minify="1" data-noptimize="1" nowprocket="1" data-pagespeed-no-defer="1"></script>
    <!-- 2. Then Google Tag Manager, gtag.js and other tags -->
  </head>
  <body>
    <app-root></app-root>
  </body>
</html>

Add it to index.html, not to the scripts array in angular.json. That array bundles scripts into the build output, which Angular places at the end of the page; CookieBeam needs to run before everything else, not after.

Route changes

The Angular router changes routes without reloading the document. The loader runs once per full page load, and the runtime, banner and consent stay in place across routes.

Wrap the hasConsent and onConsentChange helpers from Read consent in your code in a service. A signal tells templates to re-check when consent changes:

// src/app/consent.service.ts
import { Injectable, afterNextRender, signal } from '@angular/core';
import { hasConsent, onConsentChange } from './consent';

@Injectable({ providedIn: 'root' })
export class ConsentService {
  // Stays 0 during server rendering and the first browser render, so hydration matches.
  private readonly changes = signal(0);

  constructor() {
    afterNextRender(() => {
      this.changes.update((n) => n + 1);
      onConsentChange(() => this.changes.update((n) => n + 1));
    });
  }

  has(category: string): boolean {
    if (this.changes() === 0) return false;
    return hasConsent(category);
  }
}
import { Component, inject } from '@angular/core';
import { ConsentService } from './consent.service';
import { VideoEmbedComponent } from './video-embed.component';

@Component({
  selector: 'app-marketing-video',
  imports: [VideoEmbedComponent],
  template: `
    @if (consent.has('marketing')) {
      <app-video-embed />
    }
  `,
})
export class MarketingVideoComponent {
  protected readonly consent = inject(ConsentService);
}

To keep a vendor's code out of the initial bundle, wrap the gated component in a @defer block inside the @if.

For the cookie settings link, use a plain <button data-cb="show-preferencesModal">, not a routerLink. See Add a cookie settings link.

Content security policy

If your server sends a Content-Security-Policy header, see Content security policy. When your server adds a per-request nonce to Angular's scripts, add the same nonce to the CookieBeam tag and pair it with 'strict-dynamic'. Otherwise list the CookieBeam hosts in the policy.

Check the installation

  1. Build the app (ng build) and serve the output, or check the deployed site. View the page source and confirm the loader is the first script in <head>, with no async or defer.
  2. Add localhost or your preview hostname under Banner domains and publish, or the banner won't appear there.
  3. Navigate between routes. The Network panel shows a single loader.js request.
  4. Reject, then accept, a category and confirm gated components follow the choice without a reload.

Then work through Verify your installation.