1

I want to add my script in angular application which contains 'data-autoload, data-origin and data-callback' keys. I tried with _renderer2.createElement('script') method but it is throwing error.

Code

 let script = this._renderer2.createElement('script');
 script.type="application/javascript";
 script.data-autoload" =""
 script.data-origin" =""
 script.data-callback" =""

this._renderer2.appendChild(this.document.body,script)

Error

error ';' expected at

enter image description here

and same error for data-origin and data-callback

Kindly help me to resolve this issue

1 Answer 1

1

You have an invalid javascript variable.

Rules for variable name:

  • Spaces are not allowed in variable names.
  • Hyphens are not allowed in variable names.
  • Only letters, numbers, underscores, and dollar signs are permitted in variable names.
  • Case matters when it comes to variable names.
  • Variable name always starts with a letter (alphabet), underscore (_), or a dollar sign ($), i.e. any other special characters not allowed.
  • Reserved keywords are not allowed.

Reference link

The code fix for this is to use setAttribute instead.

  constructor(private _renderer2: Renderer2) {}
  
  ngOnInit() {
    let script = this._renderer2.createElement('script');
    script.setAttribute('type', 'application/javascript');
    script.setAttribute('data-autoload', '');
    script.setAttribute('data-origin', '');
    script.setAttribute('data-callback', '');
    this._renderer2.appendChild(document.body,script)
  }

Stackblitz Demo

Sign up to request clarification or add additional context in comments.

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.