svg circle - не может привязаться к cx, так как это не известное нативное свойство
Мне нужно сделать дугу прогресса на основе рассчитанного процента, я создал пользовательскую директиву для доступа к атрибутам svg от пользователя, и при обновлении этого в моем шаблоне я получаю следующую ошибку:
Невозможно связать с 'cx', так как это не известное нативное свойство
Невозможно связать с 'cy', так как это не известное нативное свойство
так далее..
Я получаю такие ошибки для всех атрибутов SVG.
Ниже мой код в нефрите:
progress-arc([size]="200", [strokeWidth]="20", [stroke]="red", [complete]="0.8")
Ниже мой код директивы:
import {Component,Input,AfterViewInit} from '@angular/core';
@Component({
selector:'progress-arc',
template:'
<svg height="100" width="100">
<circle fill="white"
cx="{{parsedSize/2}}"
cy="{{parsedSize/2}}"
r="{{radius}}"
stroke="{{stroke}}"
stroke-width="{{strokeWidthCapped}}"
stroke-dasharray="{{circumference}}"
stroke-dashoffset="{{(1 - parsedComplete) * circumference}}"/>
</svg>',
providers: [],
directives: []
})
export class ProgressArc implements AfterViewInit {
@Input('size') size:string;
@Input('strokeWidth') strokeWidth:string;
@Input('stroke') stroke:string;
@Input('complete') complete:string;
parsedStrokeWidth:number;
parsedSize:number;
parsedComplete:number;
strokeWidthCapped:number;
radius:number;
circumference:number;
ngAfterViewInit() {
this.parsedSize = parseFloat(this.size);
this.parsedStrokeWidth = parseFloat(this.strokeWidth);
this.parsedComplete = parseFloat(this.complete);
this.strokeWidthCapped = Math.min(this.parsedStrokeWidth, this.parsedSize / 2 - 1);
this.radius = Math.max((this.parsedSize - this.strokeWidthCapped) / 2 - 1, 0);
this.circumference = 2 * Math.PI * this.radius;
}
}
Может кто-нибудь сказать мне, где я иду не так?
Ответы
Ответ 1
Чтобы привязываться к атрибутам элемента SVG В angular 2 вы должны префикс их с помощью attr
:
Для вашего круга это будет:
<svg height="100" width="100">
<circle fill="white"
[attr.cx]="parsedSize/2"
[attr.cy]="parsedSize/2"
[attr.r]="radius"
[attr.stroke]="stroke"
[attr.stroke-width]="strokeWidthCapped"
[attr.stroke-dasharray]="circumference"
[attr.stroke-dashoffset]="(1 - parsedComplete) * circumference"/>
</svg>
Я не совсем уверен, что это должно быть [attr.stroke-width]
или [attr.strokeWidth]
, но дать ему выстрел