0

I'm using the bodymovin script in my project. Even though some of my animated SVG's are rendering in the browser, I'm still getting this error

Uncaught TypeError: Cannot read property 'appendChild' of undefined
    at SVGRenderer.configAnimation (bodymovin.min.js:5)
    at AnimationItem.configAnimation (bodymovin.min.js:9)
    at XMLHttpRequest.r.onreadystatechange (bodymovin.min.js:9)
    at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (zone.js:1075)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (zone.js:191)
    at ZoneTask.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:498)
    at invokeTask (zone.js:1370)
    at XMLHttpRequest.globalZoneAwareCallback (zone.js:1388)

According to this answer, I figure I should have the bodymovin script run after everything on the page has loaded. I've updated the code, taking into account the new suggestions

index.html

<!doctype html>
<html lang="en">
<head>...</head>
<body>...
<div class="full-app-body">
   <app-root></app-root>
  </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/4.10.2/bodymovin.min.js"></script>
</body>
</html>

app.component.ts

import { Component, ElementRef, OnInit, AfterViewInit } from '@angular/core';
import { Router, ActivatedRoute, RouterOutlet, Routes } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { NgClass, CommonModule } from '@angular/common';
...

export class AppComponent implements AfterViewInit {
  constructor(public router: Router, private elementRef: ElementRef) {}
...
  ngAfterViewInit() {
    const s = document.createElement('script');
    s.type = 'text/javascript';
    s.src = '/assets/js/animations.js';
    // this.elementRef.nativeElement.appendChild(s);
    document.body.appendChild(s);
  }

app.component.html

<div id='bm'></div>

animations.js

var animation = bodymovin.loadAnimation({
  container: document.getElementById('bm'),
  renderer: 'svg',
  loop: true,
  autoplay: true,
  path: './assets/images/ani/fatigue-data.json'
});
bluebrooklynbrim
  • 277
  • 4
  • 19

1 Answers1

1

as far as i understand the issue is with the line this.elementRef.nativeElement.appendChild(s);.You are trying to append something to a unknown element.U should get reference to the element you are trying to attach the script to first. Since its just a matter of adding a script,try the following code

 document.body.appendChild(s);

Hope this will help

update

Since you are using angular 4,you can use Renderer2 for solving your purpose as suggested here

Vikhyath Maiya
  • 3,122
  • 3
  • 34
  • 68