2017-04-24 77 views
0

我有一个SVG元素,我想用ngFor绘制。 SVG只包含行,所以理论上应该是可行的。 目前,我有以下代码:Angular2 - NgFor使用SVG

我-component.js:

import {Component} from 'angular2/core'; 

@Component({ 
    selector: 'my-component', 
    templateUrl: 'my-component.html' 
}) 

export class MyComponent{ 
    lines: [ 
    { weight: 0.4, x1: 86.69, y1: 1, x2: 98.91, y2: 1 }, 
    { weight: 0.5, x1: 85.31, y1: 9.67, x2: 98.23, y2: 9.67 } 
    ] 
} 

我组分-HTML:

<svg id="lines" viewBox="0 0 320.6 542.59" *ngFor='line of lines'> 
    <line class="line" [attr.x1]="{{ x1 }}" [attr.y1]="{{ y1 }}" [attr.x2]="{{ x2 }}" [attr.y2]="{{ y2 }}"/> 
</svg> 

它不工作,我相信我有多个语法错误,但我不知道哪些错误。

回答

3

你不应该混合绑定和插值。试试这个:

<svg id="lines" viewBox="0 0 320.6 542.59" > 
    <line *ngFor="let line of lines" class="line" [attr.x1]="line.x1" [attr.y1]="line.y1" [attr.x2]="line.x2" [attr.y2]="line.y2"/> 
</svg> 

而在你的组件也改变:=

export class MyComponent{ 
    lines = [ // here 
    { weight: 0.4, x1: 86.69, y1: 1, x2: 98.91, y2: 1 }, 
    { weight: 0.5, x1: 85.31, y1: 9.67, x2: 98.23, y2: 9.67 } 
    ]; 
} 
+0

完美的作品,非常感谢!只要4分钟结束就会接受。 – IIMaxII