How to load SVG synchronous by using snap.svg in Angular 2 component constructor?
NickName:John Ask DateTime:2016-05-28T01:04:36

How to load SVG synchronous by using snap.svg in Angular 2 component constructor?

Please point me. I know snap.load() is a async call but I want to make it synchronously inside of component constructor so that when construction finish I have have all the svg elements ready for use, or assign the elements to child components. Something like this. So basically, how can I block constructor until snap.load completed?

import { Component } from '@angular/core';
import { ChildComponent } from 'child.component';


@Component({
  selector: 'parent',
  directives: [ChildComponent]
})
export class AppComponent {
  
  svgPath: string = "assets/svg/sprites.svg"
  SVGElements: any;

  constructor(){
    var self = this;
    
    Snap.load(svgPath, function(items) {
      
      items.selectAll().forEach(function(item) {
        var clone = item.clone()        
        self.SVGElements.push(clone);
      });      

    }

    //this show empty, I know because snap.load is an async call 
    console.log(this.SVGElements);

  }

  ngOnInit() {
    //I want to do something with this.SVGElements here
  }
}

Copyright Notice:Content Author:「John」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37488913/how-to-load-svg-synchronous-by-using-snap-svg-in-angular-2-component-constructor

More about “How to load SVG synchronous by using snap.svg in Angular 2 component constructor?” related questions

How to load SVG synchronous by using snap.svg in Angular 2 component constructor?

Please point me. I know snap.load() is a async call but I want to make it synchronously inside of component constructor so that when construction finish I have have all the svg elements ready for u...

Show Detail

Snap.svg - How to load SVGs synchronous?

Knowing that Snap.load(); is asynchronous, I'm looking for a way to append the loaded files in the requested order. This is the code I'm using: s = Snap(800, 800); function loadSvg(url) { Snap.

Show Detail

How to use Snap.svg with Angular v4.0

I don't know how to use snap.svg with Angular (created with angular-cli). I've tried to call Snap.svg in the index.html with CDN, import it in the component by adding : import 'snapsvg' but I alway...

Show Detail

How to using Snap.svg and Snap.js via angular-snap together?

I try to use two dependencies: "snap.svg": "~0.2.0", # snapsvg.io "angular-snap": "~1.4.1", # jtrussell.github.io/angular-snap.js/ How I guess, it seems angular-snap uses a global Snap object

Show Detail

Load 2 svg files in snap.svg with a onclick function

At the moment i am loading up 1 SVG File with snap.svg and i manipulate it a little bit. But now I want to open up another SVG File when i click on a rectangle in my already loaded SVG file. Is that

Show Detail

Using snap.svg.js in Angular2 Component

I'm trying to build a component in Angular 2 that makes use of snap.svg. I seem incapable of getting a functional svg object to work even in the most simple of cases. I'm importing snap.svg in my i...

Show Detail

Snap.svg in React - how to access the svg in a lifecycle method?

I have an svg map component which I want to update whenever the props change. Adding and subtract classes on some of the paths... that sort of thing. Snap.svg seemed to be the way to go. If someone

Show Detail

Is it possible to load part of svg file using snap.svg?

I have svg <svg><g id="g1">polygons here...</g><g id="g2">polygons here...</g></svg> Now I'm loading using snap.svg &am

Show Detail

Unit testing AngularJS directive wrapping Snap.svg

I'm trying to create an Angular directive to wrap Snap.svg functionality, but I'm having trouble with unit testing it. So far I have a directive that looks like this: 'use strict'; angular.module('

Show Detail

How to use Snap.svg with Angular v4.0 (error: circle is not a function)

I don't know how to use Snap.svg with Angular v4.0, so I read this post. I tried the solution of @moeabdol (npm install ..., scripts: ..., declare ..., s.circle(...)), but I get the following error...

Show Detail