svg circle - 无法绑定到cx,因为它不是已知的本机属性

问题描述 投票:28回答:1

我需要根据计算的百分比做一个进度弧,我创建了一个自定义指令来访问用户的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;
  }
}

谁能告诉我哪里出错了?

javascript angular typescript angular2-directives
1个回答
88
投票

为了绑定到SVG元素属性在角度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],但是给它一个机会

© www.soinside.com 2019 - 2024. All rights reserved.