0
<script data-jsd-embedded data-base-url="" data-key="" type="text/javascript" src="js-text-sample.js"></script>

Above is my sample script tag which is on index.html

I want to hide it inside an Angular component.

The angular component will fetch a config first from the API. Then it will validate if it needs to hide the script or not.

The script is a embedded widget that I need to hide if its false on the config API.

How do I hide it from the component code?

5
  • try: <script *ngIf="shouldHide" data-jsd-embedded data-base-url="" data-key="" type="text/javascript" src="js-text-sample.js"></script> Commented Oct 17, 2022 at 5:54
  • @luke jon we cannot use ng-if inside index.html in Angular project. Commented Oct 17, 2022 at 6:00
  • You could create the script-tag inside of angular, if it should be included. See an example here: stackoverflow.com/a/46924372/11028838 Commented Oct 17, 2022 at 6:24
  • A script tag is not visible, so what do you mean by "hiding" a script tag? Commented Oct 17, 2022 at 6:35
  • I mean remove the script tag. Commented Oct 17, 2022 at 7:28

2 Answers 2

1

Instead of placing script tag and worrying about after logic to hide/show, why don't you create script tag at runtime based on result.

import { Renderer2, OnInit, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';

class MyComponent implements OnInit {

    constructor(
        private _renderer2: Renderer2, 
        @Inject(DOCUMENT) private _document: Document
    ) { }

    public ngOnInit() {

        let script = this._renderer2.createElement('script');
        script.src='_SOURCE_HERE_'
        `;

        this._renderer2.appendChild(this._document.body, script);
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

you can also create a method which can handle add/remove for you of script tag by id, sorry I didn't show above code
how do I add these elements on the <script> when appending? (data-jsd-embedded data-base-url="" data-key="")
script.setAttribute('data-jsd-embedded', ''); script.setAttribute('data-base-url', ''); script.setAttribute('data-key', '');
I tried it but it doesn't work
it was working for me give me a moment I will give you stackblitz link
|
0

You can do it the clasic, javascript way. When you get your response from the api, query the DOM to get a reference to the element and remove it:

const element = document.getElementById('widgetContainerId');
element.parentElement.removeChild(element);

This will remove the widget from the DOM.

If later during the lifecycle of the application you want to show it again, then you should not remove the whole element from the DOM, but hide it, maybe with display: none;:

const element = document.getElementById('widgetContainerId');
element.style.display = 'none';

Whenever you need it to be visible again, update the display property to something else, like block:

element.style.display = 'block';

2 Comments

this doesn't work. The widget still shows.
You should hide (or remove) the widget itself, not the script element.

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.