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

5
  • How about putting the angular-element script in scripts in angular.json? Commented Jun 6, 2018 at 18:03
  • 1
    @ChauTran I tried include in scripts but it doesn`t work. I think i am missing something. Commented Jun 6, 2018 at 18:09
  • @MoazzamKhanany have you come to any solution? Commented May 3, 2019 at 13:29
  • @PavanShukla yes, but it was a bit complicated. I will try to post it here in couple of days. Commented May 3, 2019 at 14:26
  • @MoazzamKhan Hi, we're facing the same issue. Can you post your solution please? Commented Oct 21, 2020 at 5:54

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

it worked for me too, when placed within index.html, but it does not work for any other component (eg, app.component.html)
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 ]
})

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.