5

I have an angular element, goes with custom tagname - fancy-button. How can I embed fancy-button in a angular application?

I have tried the following ways, but none of them worked -

  1. Imported angular-element script in index.html/angular.json[scripts] and embedded fancy-button in app.component.html (root component running in angular application)

  2. Imported angular-element script in app.component.html and also embedded fancy-button in app.component.html (root component running in angular application)

Thanks

Moazzam Khan
  • 3,130
  • 2
  • 20
  • 35

3 Answers3

1

To index.html, I added:

<script type="text/javascript" src="assets/cappapproot.js"></script>

(cappapproot.js is the name of my Angular Element js)

This caused an Uncaught Error: Zone already loaded. To resolve this error, in pollyfills.ts I commented out the line:

import 'zone.js/dist/zone';  // Included with Angular CLI.
Vivek
  • 322
  • 1
  • 13
RichardZ
  • 345
  • 2
  • 6
  • 18
  • it worked for me too, when placed within index.html, but it does not work for any other component (eg, app.component.html) – Vivek Jan 24 '20 at 08:46
1

in the end, I managed to implement the custom element in my application. You should do the next steps:

  1. In your angular.json add the script file in your selected project:
    ...,
    "projects": {
        "my-awesome-project": {
        ...,
        "architect": {
            "build": {
                "options":{
                    ...,
                    "scripts": [
                        ...,
                        "[path to your awesome Element js]/[awesome-element].js"
                    ],
                    ...
                 },
             ...
         },
         ...
     }

  1. In your polyfills files include import 'zone.js/dist/zone'; // Included with Angular CLI. at the end of the file.

  2. In the module you want to include your Angular element you must add the CUSTOM_ELEMENTS_SCHEMA in the schemas parameter of the decorator:

@NgModule({
  declarations: [...],
  imports: [
    CommonModule,
    ...
  ],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
afiugud
  • 11
  • 1
0

Are you absolutely sure your using the correct element name? Can you post your component + usage?

Steoates
  • 3,058
  • 5
  • 27
  • 43