Giter VIP home page Giter VIP logo

angularcomponents_sample9-elements's Introduction

Angular_Elements

https://angular.io/guide/elements

https://stackblitz.com/~/github.com/luiscoco/AngularComponents_Sample9-Elements

In Angular, "elements" refer to custom HTML elements that are created and used within an Angular application. These elements are also known as Angular Elements or Angular Custom Elements. They allow you to encapsulate Angular components and use them in non-Angular applications or frameworks, such as plain HTML, React, or Vue.

Angular Elements are created by compiling an Angular component into a standalone JavaScript bundle. This bundle can then be included in any HTML page, and the custom element can be used like any other HTML element.

Here's a simple code sample that demonstrates the creation of an Angular Element:

1. Create an Angular component:

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

@Component({
  selector: 'my-element',
  template: `
    <h1>{{ message }}</h1>
    <p>Counter: {{ counter }}</p>
    <button (click)="increment()">Increment</button>
  `,
})
export class MyElementComponent {
  @Input() message: string;
  counter: number = 0;

  increment() {
    this.counter++;
  }
}

2. Create an Angular module and define the component as an entry component:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { createCustomElement } from '@angular/elements';
import { MyElementComponent } from './my-element.component';

@NgModule({
  imports: [BrowserModule],
  declarations: [MyElementComponent],
  entryComponents: [MyElementComponent],
})
export class AppModule {
  constructor() {
    const myElement = createCustomElement(MyElementComponent, { injector: this.injector });
    customElements.define('my-element', myElement);
  }

  ngDoBootstrap() {}
}

3. Bootstrap the Angular module (main.ts):

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

4. Build the Angular project as an Angular Element:

ng build --prod --output-hashing none --single-bundle true

This will generate a single JavaScript bundle (main.js) that includes the Angular Element.

5. Include the Angular Element in an HTML page:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Angular Element Demo</title>
</head>
<body>
  <my-element message="Hello Angular Element"></my-element>
  <script src="path/to/main.js"></script>
</body>
</html>

In this example, the MyElementComponent is transformed into an Angular Element using the createCustomElement function. The Angular module is bootstrapped with the ngDoBootstrap method. Finally, the main.js bundle is included in an HTML page, and the my-element custom element is used with the message input property.

When the HTML page is loaded, the Angular Element will be rendered, and you'll see the message "Hello Angular Element" along with a counter that can be incremented by clicking the "Increment" button.

Note that Angular Elements require Angular version 6 or above and require additional setup steps for compatibility with different frameworks or environments.

angularcomponents_sample9-elements's People

Contributors

luiscoco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.