Partager via


Use Fluent UI Web Components with Angular

Fluent UI Web Components integrate nicely with Angular. Let's take a look at how you can set up an Angular project, starting from scratch.

Setting up the Angular project

First, you'll need to make sure that you have Node.js installed. You can learn more and download that on the official site.

With Node.js installed, you can run the following command to install the Angular CLI:

npm install -g @angular/cli

With the CLI installed, you have access to the ng command-line interface. This can be used to create a new Angular project. For example, to create a new Angular App named "fluent-angular", you would use the following command:

ng new fluent-angular

Follow the prompts, answering each question in turn. When the CLI completes, you should have a basic runnable Angular application.

Configuring packages

Next, we'll install the Fluent packages, along with supporting libraries. To do that, run this command from your new project folder:

npm install --save @fluentui/web-components @microsoft/fast-element lodash-es

Using the components

With all the basic pieces in place, let's run our app in dev mode with ng serve --open. The Angular CLI should build your project and make it available on localhost. Right now, it displays a basic welcome message, since we haven't added any code or interesting HTML. Let's change that.

First, open your src/main.ts file and add the following code:

import { provideFluentDesignSystem, fluentCard, fluentButton, fluentTextField } from '@microsoft/fluent-components';

provideFluentDesignSystem().register(fluentCard(), fluentButton(), fluentTextField());

This code uses the Fluent Design System to register <fluent-card>, <fluent-button> and <fluent-text-field> components. Once you save, the dev server will rebuild and refresh your browser. However, you still won't see anything. To get some UI showing up, we need to write some HTML that uses our components. Replace the HTML template in your app/app.component.html file with the following markup:

<fluent-card>
  <h2>{{title}}</h2>
  <fluent-text-field
    [(ngModel)]="exampleTextField"
    name="exampleTextField"
    ngDefaultControl
    placeholder="Enter Some Text"
  ></fluent-text-field>
  <fluent-button appearance="accent" (click)="onClick()">Click Me</fluent-button>
</fluent-card>

Replace the code in your app/app.component.ts file contents with this:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  title = 'fluent-angular';

  exampleTextField = '';

  onClick() {
    console.log(this.exampleTextField);
  }
}

To allow an NgModule to contain Non-Angular element names, add the following code in your app/app.module.ts file:

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

@NgModule({
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})

To add a splash of style, replace the app/app.component.css file contents with this:

fluent-card {
  padding: 16px;
  display: flex;
  flex-direction: column;
}

fluent-text-field {
  margin-bottom: 12px;
}

h2 {
  font-size: var(--type-ramp-plus-5-font-size);
  line-height: var(--type-ramp-plus-5-line-height);
}

fluent-card > fluent-button {
  align-self: flex-end;
}

Note

Third party controls require a ControlValueAccessor for writing a value and listening to changes on input elements. Add ngDefaultControl attribute to your component to have two-way binding working with FormControlDirective, FormControlName, or NgModel directives:

<fluent-text-field placeholder="name" id="name" formControlName="name" ngDefaultControl></fluent-text-field>

Congratulations! You're now set up to use Fluent and Angular!

Next steps