Svg using angular 2 and typescript not rendering w

2019-06-04 07:02发布

I'm playing around with Angular2, using typescript and there is something that is not rendering as I hoped for, I don't know what I'm doing wrong.

This code is working

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: `
        <svg viewBox="0 0 900 500" preserveAspectRatio="xMidYMid meet">
            <svg:circle cx=50 cy=50 r=10 />
            <svg:circle cx=71 cy=71 r=20 />
            <svg:circle cx=106 cy=106 r=30 />
        </svg>
    `
})
export class AppComponent {
}

But when I'm trying to bind it on an object with *ngFor, it's not rendering...

@Component({
    selector: 'my-app',
    template: `
        <svg viewBox="0 0 900 500" preserveAspectRatio="xMidYMid meet">
            <svg:circle *ngFor="#circle of circles"
                  [attr.cx]="circle.x"
                  [attr.cy]="circle.y"
                  [attr.r]="circle.radius" />
        </svg>
    `
})
export class AppComponent {
    circles = [
        {x: 50, y: 50, radius: 10},
        {x: 75, y: 75, radius: 20},
        {x: 115, y: 115, radius: 30}
    ];
}

I'm trying to follow this tutorial (http://teropa.info/blog/2016/02/28/metabubbles-generative-art-with-angular-2.html#drawing-svg-circles) using typescript instead ...


Seems to be a duplicate of AngularJS 2 and SVG <ellipse> elements not rendered

Dom elements not generated for every iteration and properties(attributes) not generated...


THIS IS NOW FIXED It works in Chrome, but not in IE 11. IE Console was not displaying the javascript errors and chrome console was. That how I managed to fix my issue trying to solve the javascript errors I had.

I turned out to change the SystemJS config. Instead of using the following config as stated there https://angular.io/docs/ts/latest/quickstart.html#!#tsconfig

System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });

I used it :

System.config({
    transpiler: 'typescript',
    typescriptOptions: {
        emitDecoratorMetadata: true
    },
    map: {
        app: "./app"
    },
    packages: {
        app: {
            main: './main',
            defaultExtension: 'js'
        }
    }
});

That's one part I'm not understanding very well, are those typescriptoptions are overrideing tsconfig.json file? I don't know yet... I don't know yet what is this mapping for and what are the packages, but at least I can go on with my learning

标签: svg angular
2条回答
做个烂人
2楼-- · 2019-06-04 07:50

you can try this.

defining your circle component

@Component({
    selector: 'g[circle]',
    templateUrl: './circle.component.html',
    styleUrls: ['./circle.component.scss']
})

circle.component.hmtl :

<svg:circle
            [attr.cx]="circle?.x" 
            [attr.cy]="circle?.y" 
            [attr.r]="radius" 

            [attr.fill]="circle?.fill"
             />

and your root html will be like

<svg>
<g circle *ngFor="let circle of circles" 
                    [attr.fill-opacity]="opacity" 
                    [attr.transform]="scale" 
                    [circle]="circle"
                     />
</svg>

similarly you can use all the other svg shapes by creating similar components with selector as defied above.

You can use this library which provides all the svg elements

https://www.npmjs.com/package/angular-svg

查看更多
Anthone
3楼-- · 2019-06-04 07:53

I'm new to angular2 but this works for me, maybe you have to add:

  • directives:[NgFor]

  • import { NgFor } from 'angular2/common';

sorry for my English hope it will help you.

import { Component } from 'angular2/core';
import { NgFor } from 'angular2/common
..//
@Component({
    selector: 'test',
    directives:[NgFor],
    template: `
               <svg viewBox="0 0 900 500" preserveAspectRatio="xMidYMid meet">
                    <svg:circle *ngFor="#circle of circles"
                        [attr.cx]="circle.x"
                        [attr.cy]="circle.y"
                        [attr.r]="circle.radius" />
                   </svg>`

})

Update:

I have also tried without

  • directives:[NgFor]

  • import { NgFor } from 'angular2/common';

    and it works.

enter image description here


package.json info

..//
"license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15"
  },
  ..//
查看更多
登录 后发表回答