@builder.io/angular

中文文档
5.0.2发布于 2025/02/14MITTypeScriptESM + CJS近 30 天下载 1.9万

Use the Builder Angular SDK to use Angular with Builder. You can get started by heading over to Builder's official documentation or digging right into the code in this directory. The official documentation provides more explicit instructions, while this R

原始使用文档
文档目录10 节
  1. Option 1 (for those new to Angular): Use the Builder offici…
  2. Option 2 (for the Angular aficionado): Use the brief notes …
  3. Usage
  4. Custom landing pages in your code
  5. Using custom fields
  6. Builder sections within existing pages
  7. Use your Angular components in your Builder pages
  8. Editable Regions within your custom components
  9. Passing data and context down
  10. Example projects

Builder Angular SDK

Use the Builder Angular SDK to use Angular with Builder. You can get started by heading over to Builder's official documentation or digging right into the code in this directory. The official documentation provides more explicit instructions, while this README shares more general pointers.

Option 1 (for those new to Angular): Use the Builder official documentation

For a step-by-step guide, see the Angular instructions in Builder's official Integrating Pages documentation. While we recommend starting with Page building, you can also integrate sections and data:

Integrate Page Building Integrate Section Building Integrate CMS Data
CTA to integrate page buliding CTA to integrate section buliding CTA to integrate CMS data

Option 2 (for the Angular aficionado): Use the brief notes below

If instead, you prefer to dive right into the code, stay here for some pointers for using the Builder Angular SDK.

Usage

Install:

npm install @builder.io/angular
npm install @angular/elements

Add the module:

import { BuilderModule } from '@builder.io/angular';

@NgModule({
  ...
  imports: [ BuilderModule.forRoot('YOUR_API_KEY') ],
  ...
})
export class MyAppModule { }

NOTE: You can get YOUR_API_KEY from https://builder.io/account/space.

And then add the component wherever you like:

<!-- The model input can be any model of yours -->
<builder-component model="page" (load)="contentLoaded($event)" (error)="contentError($event)">
  <!-- Default content inside the tag shows while the builder content is fetching -->
  <div class="spinner"></div>
</builder-component>

Then, update your model's preview URL to enable on-site editing like in this guide, and you are done!

Next, see the below info for more advanced usage, as well as Intro to Models for creating custom models, and Search Enging Optimization for SEO optimizing your content. (For Angular use the data from the load output to get the custom field data.)

Custom landing pages in your code

Replace your 404 component with something like the below to allow creating new pages in Builder easily:

<!-- The model input can be any model of yours -->
<builder-component
  *ngIf="!noBuilderPageForUrl"
  model="page"
  (load)="noBuilderPageForUrl = $event ? false : true"
  (error)="noBuilderPageForUrl = true"
>
  <!-- Default content inside the tag shows while the builder content is fetching -->
  <div class="spinner"></div>
</builder-component>
<my-404-component *ngIf="noBuilderPageForUrl"> </my-404-component>

Using custom fields

Custom fields are a powerful feature when using customized models, for all sorts of customization, such as SEO optimization of your content.

<builder-component model="page" (load)="contentLoaded($event)">
  <!-- Default content inside the tag shows while the builder content is fetching -->
  <div class="spinner"></div>
</builder-component>
contentLoaded(data) {
  // Data object (via the output $event) includes your custom fields, e.g. if you have a custom field named
  // "title"
  document.title = data.data.title
}

Builder sections within existing pages

With section models you can use Builder.io components in/around existing pages (aka it doesn't have to control the whole page). See info on making custom models for this here

<!-- The first part of your page -->
<builder-component model="announcement-bar">Loading..</builder-component>
<!-- the rest of your page -->

You can then use queries and targeting to customize what content loads where

Use your Angular components in your Builder pages

You can drag and drop to add your Angular components in the Builder editor with a minimal tag like below:

import { BuilderBlock } from '@builder.io/angular';
import { Component, Input } from '@angular/core';

@BuilderBlock({
  tag: 'custom-thing',
  name: 'Custom thing',
  inputs: [
    {
      name: 'name',
      type: 'string',
    },
  ],
})
@Component({
  selector: 'custom-thing',
  template: 'Hello: {{name}}',
})
export class CustomThing {
  @Input()
  name = '';
}

Note that custom Angular components use Angular elements and render in the browser only (no server-side rendering).

If you need server-side rendering in reusable components with Builder, consider using symbols

See here for full detail on input types available.

Visual of using your Angular component in Builder

Editable Regions within your custom components

Visual of adding custom editable regions within your components in Builder
  • Register inputs for each of your editable sections of type blocks
  • Use builder-blocks-outlet component to render those blocks withing your component template.
@Component({
  selector: 'custom-thing',
  template: `
    <h2>Section A</h2>
    <builder-blocks-outlet
      [blocks]="sectionA"
      [builderState]="builderState"
      [builderBlock]="builderBlock"
      dataPath="component.options.sectionA"
    ></builder-blocks-outlet>
    <h2>Section B</h2>
    <builder-blocks-outlet
      [blocks]="sectionB"
      [builderState]="builderState"
      [builderBlock]="builderBlock"
      dataPath="component.options.sectionB"
    ></builder-blocks-outlet>
  `,
})
export class CustomThing implements OnChanges {
  @Input()
  name = '';

  @Input()
  builderBlock = null;

  @Input()
  builderState = null;

  @Input()
  sectionA = null;

  @Input()
  sectionB = null;
}

BuilderBlock({
  tag: 'custom-thing',
  name: 'Custom thing',
  canHaveChildren: true,
  inputs: [
    {
      name: 'name',
      type: 'string',
    },
    {
      name: 'sectionA',
      type: 'blocks',
      hideFromUI: true,
      helperText: 'This is an editable region where you can drag and drop blocks.',
      defaultValue: [
        {
          '@type': '@builder.io/sdk:Element',
          component: {
            name: 'Text',
            options: {
              text: 'Section A Editable in Builder...',
            },
          },
          responsiveStyles: {
            large: {
              display: 'flex',
              flexDirection: 'column',
              position: 'relative',
              flexShrink: '0',
              boxSizing: 'border-box',
              marginTop: '20px',
              lineHeight: 'normal',
              height: 'auto',
              textAlign: 'center',
            },
          },
        },
      ],
    },
    {
      name: 'sectionB',
      type: 'blocks',
      hideFromUI: true,
      helperText: 'This is an editable region where you can drag and drop blocks.',
      defaultValue: [
        {
          '@type': '@builder.io/sdk:Element',
          component: {
            name: 'Text',
            options: {
              text: 'Section B Editable in Builder...',
            },
          },
          responsiveStyles: {
            large: {
              display: 'flex',
              flexDirection: 'column',
              position: 'relative',
              flexShrink: '0',
              boxSizing: 'border-box',
              marginTop: '20px',
              lineHeight: 'normal',
              height: 'auto',
              textAlign: 'center',
            },
          },
        },
      ],
    },
  ],
})(CustomThing);

Passing data and context down

You can also pass data and functions down to the Builder component to use in the UIs (e.g. bind data values to UIs e.g. for text values or iterating over lists, and actions to trigger for instance on click of a button)

All data passed down is available in Builder actions and bindings as state.*, for instance in the below example state.resources, etc will be available

@Component({
  selector: 'app-root',
  template: '<builder-component [options]="options" [context]="context" [data]="data" model="page"></builder-component>',
})
export class AppComponent {
  options: any = {
    cacheSeconds: 1,
    data: {
      locale: 'en-US',
    },
  };

  data = {
    resources: [ { foo: 'bar'} ]
  };

  context= {
    myFunction: (text: string) => alert(text),
  }

You can also pass down functions, complex data like custom objects and libraries you can use context. Context passes all the way down (e.g. through symbols, etc). This data is not observed for changes and mutations

Context is available in actions and bindings as context.*, such as context.myFunction('hello world') in the example above

Example projects

To see a full example integration see here for a simple Angular + Builder.io example project, or here for an Angular universal example

版本与发布记录

@builder.io/angular 共发布 755 个版本,首次发布于 2018/02/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.2latest2025/02/141.7 年前
5.0.12025/01/151.8 年前
5.0.02024/12/211.9 年前
3.0.2v3-lts2024/08/072 年前
4.0.02024/08/032 年前
3.1.1-0dev2024/07/312 年前

其他发布通道:nightly → 0.1.14-4,dev → 3.1.1-0,v3-lts → 3.0.2。

关于 @builder.io/angular 的常见问题

@builder.io/angular 是什么?

Use the Builder Angular SDK to use Angular with Builder. You can get started by heading over to Builder's official documentation or digging right into the code in this directory. The official documentation provides more explicit instructions, while this R

如何安装 @builder.io/angular?

在项目目录运行 npm install @builder.io/angular,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:rxjs(>= 2)、zone.js(0.14.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@builder.io/angular 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@builder.io/angular 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@builder.io/angular 可以商用吗?

@builder.io/angular 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@builder.io/angular 的下载量有多少?

根据 npm 官方下载统计,@builder.io/angular 在 2026/10/09 – 2026/11/07 这 30 天内被下载 18,866 次,日均约 629 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@builder.io/angular 还在维护吗?

最新版本 5.0.2 发布于 2025/02/14(1.7 年前);至今共发布 755 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/02/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。