简体   繁体   English

如何使用角度4中的ngFor动态渲染svg

[英]How to dynamically render svg using ngFor in angular 4

I have a coordinate list. 我有一个坐标列表。 I want to dynamically render the SVG image in angular 4. I am Getting below error. 我想以角度4动态渲染SVG图像。我正在低于错误。 please help me to resolve. 请帮我解决。

HTML HTML

 <svg height="100%" width="100%" (click)="addTag($event)" *ngFor="let drawLine of drawLineList">
        <circle appTagDraggable [cx]="drawLine.x1" [cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="onCricleDragStart($event)" (dragMove)="onCricleDragMove($event)" (dragEnd)="onCricleDragEnd($event)" />
        <line [x1]="drawLine.x1" [y1]="drawLine.y1" [x2]="drawLine.x2" [y2]="drawLine.y2" style="stroke:rgb(255,0,0);stroke-width:2;stroke-dasharray: 5,3" />
        <circle appTagDraggable [cx]="drawLine.x1" [cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="onCricleDragStart($event)" (dragMove)="onCricleDragMove($event)" (dragEnd)="onCricleDragEnd($event)" />
 </svg>

core.js:1449 ERROR Error: Uncaught (in promise): Error: Template parse errors: Can't bind to 'cx' since it isn't a known property of ':svg:circle'. core.js:1449 ERROR错误:未捕获(在承诺中):错误:模板解析错误:无法绑定到'cx',因为它不是':svg:circle'的已知属性。 ("click)="addTag($event)" *ngFor="let drawLine of drawLineList"> ][cx]="drawLine.x1" [cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="o"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@251:36 Can't bind to 'cy' since it isn't a known property of ':svg:circle'. ("nt)" *ngFor="let drawLine of drawLineList"> ][cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="onCricleDragStart($e"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@251:55 Can't bind to 'x1' since it isn't a known property of ':svg:line'. ("t)" (dragMove)="onCricleDragMove($event)" (dragEnd)="onCricleDragEnd($event)" /> ][x1]="drawLine.x1" [y1]="drawLine.y1" [x2]="drawLine.x2" [y2]="drawLine.y2" style="stroke:rgb(255,0,0"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:18 Can't bind to 'y1' since it isn't a known property of ':svg:line'. ("ricleDragMove($event)" (dragEnd)="onCricleDragEnd($event)" /> ][y1]="drawLine.y1" [x2]="drawLine.x2" (“click”=“addTag($ event)”* ngFor =“let drawLine of drawLineList”>] [cx] =“drawLine.x1”[cy] =“drawLine.y1”r =“5”stroke =“black “stroke-width =”3“fill =”red“(dragStart)=”o“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@251:36无法绑定到'cy',因为它不是已知属性':svg:circle'。(“nt)”* ngFor =“let drawLine of drawLineList”>] [cy] =“drawLine.y1”r =“5”stroke =“black”stroke-width =“ 3“fill =”red“(dragStart)=”onCricleDragStart($ e“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@251:55无法绑定到'x1',因为它不是'x1'的已知属性':svg:line'。(“t)”(dragMove)=“onCricleDragMove($ event)”(dragEnd)=“onCricleDragEnd($ event)”/>] [x1] =“drawLine.x1”[y1] = “drawLine.y1”[x2] =“drawLine.x2”[y2] =“drawLine.y2”style =“stroke:rgb(255,0,0”):ng:///GlobalSettingsModule/ImageCaptureComponent.html@252 :18无法绑定到'y1',因为它不是':svg:line'的已知属性。(“ricleDragMove($ event)”(dragEnd)=“onCricleDragEnd($ event)”/>] [y1 ] =“drawLine.y1”[x2] =“drawLine.x2” [y2]="drawLine.y2" style="stroke:rgb(255,0,0);stroke-width:2;st"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:37 Can't bind to 'x2' since it isn't a known property of ':svg:line'. [y2] =“drawLine.y2”style =“stroke:rgb(255,0,0); stroke-width:2; st”):ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:37不能绑定到'x2',因为它不是':svg:line'的已知属性。 ("t)" (dragEnd)="onCricleDragEnd($event)" /> ][x2]="drawLine.x2" [y2]="drawLine.y2" style="stroke:rgb(255,0,0);stroke-width:2;stroke-dasharray: 5,3"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:56 Can't bind to 'y2' since it isn't a known property of ':svg:line'. (“t)”(dragEnd)=“onCricleDragEnd($ event)”/>] [x2] =“drawLine.x2”[y2] =“drawLine.y2”style =“stroke:rgb(255,0,0) ; stroke-width:2; stroke-dasharray:5,3“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:56无法绑定到'y2',因为它不是'的已知属性': SVG:线”。 ("icleDragEnd($event)" /> ][y2]="drawLine.y2" style="stroke:rgb(255,0,0);stroke-width:2;stroke-dasharray: 5,3" /> <"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:75 Can't bind to 'cx' since it isn't a known property of ':svg:circle'. ("="stroke:rgb(255,0,0);stroke-width:2;stroke-dasharray: 5,3" /> ][cx]="drawLine.x1" [cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="o"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@253:36 Can't bind to 'cy' since it isn't a known property of ':svg:circle'. (“icleDragEnd($ event)”/>] [y2] =“drawLine.y2”style =“stroke:rgb(255,0,0); stroke-width:2; stroke-dasharray:5,3”/> <“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:75无法绑定到'cx',因为它不是':svg:circle'的已知属性。(”=“stroke:rgb( 255,0,0); stroke-width:2; stroke-dasharray:5,3“/>] [cx] =”drawLine.x1“[cy] =”drawLine.y1“r =”5“stroke =”黑色“stroke-width =”3“fill =”red“(dragStart)=”o“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@253:36无法绑定到'cy',因为它不是'svg:circle'的已知属性。 ("0);stroke-width:2;stroke-dasharray: 5,3" /> ][cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="onCricleDragStart($e"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@253:55 Error: Template parse errors: Can't bind to 'cx' since it isn't a known property of ':svg:circle'. (“0”; stroke-width:2; stroke-dasharray:5,3“/>] [cy] =”drawLine.y1“r =”5“stroke =”black“stroke-width =”3“fill = “red”(dragStart)=“onCricleDragStart($ e”):ng:///GlobalSettingsModule/ImageCaptureComponent.html@253:55错误:模板解析错误:无法绑定到'cx',因为它不是已知的':svg:circle'的属性。 ("click)="addTag($event)" *ngFor="let drawLine of drawLineList"> ][cx]="drawLine.x1" [cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="o"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@251:36 Can't bind to 'cy' since it isn't a known property of ':svg:circle'. ("nt)" *ngFor="let drawLine of drawLineList"> ][cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="onCricleDragStart($e"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@251:55 Can't bind to 'x1' since it isn't a known property of ':svg:line'. ("t)" (dragMove)="onCricleDragMove($event)" (dragEnd)="onCricleDragEnd($event)" /> ][x1]="drawLine.x1" [y1]="drawLine.y1" [x2]="drawLine.x2" [y2]="drawLine.y2" style="stroke:rgb(255,0,0"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:18 Can't bind to 'y1' since it isn't a known property of ':svg:line'. ("ricleDragMove($event)" (dragEnd)="onCricleDragEnd($event)" /> ][y1]="drawLine.y1" [x2]="drawLine.x2" (“click”=“addTag($ event)”* ngFor =“let drawLine of drawLineList”>] [cx] =“drawLine.x1”[cy] =“drawLine.y1”r =“5”stroke =“black “stroke-width =”3“fill =”red“(dragStart)=”o“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@251:36无法绑定到'cy',因为它不是已知属性':svg:circle'。(“nt)”* ngFor =“let drawLine of drawLineList”>] [cy] =“drawLine.y1”r =“5”stroke =“black”stroke-width =“ 3“fill =”red“(dragStart)=”onCricleDragStart($ e“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@251:55无法绑定到'x1',因为它不是'x1'的已知属性':svg:line'。(“t)”(dragMove)=“onCricleDragMove($ event)”(dragEnd)=“onCricleDragEnd($ event)”/>] [x1] =“drawLine.x1”[y1] = “drawLine.y1”[x2] =“drawLine.x2”[y2] =“drawLine.y2”style =“stroke:rgb(255,0,0”):ng:///GlobalSettingsModule/ImageCaptureComponent.html@252 :18无法绑定到'y1',因为它不是':svg:line'的已知属性。(“ricleDragMove($ event)”(dragEnd)=“onCricleDragEnd($ event)”/>] [y1 ] =“drawLine.y1”[x2] =“drawLine.x2” [y2]="drawLine.y2" style="stroke:rgb(255,0,0);stroke-width:2;st"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:37 Can't bind to 'x2' since it isn't a known property of ':svg:line'. [y2] =“drawLine.y2”style =“stroke:rgb(255,0,0); stroke-width:2; st”):ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:37不能绑定到'x2',因为它不是':svg:line'的已知属性。 ("t)" (dragEnd)="onCricleDragEnd($event)" /> ][x2]="drawLine.x2" [y2]="drawLine.y2" style="stroke:rgb(255,0,0);stroke-width:2;stroke-dasharray: 5,3"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:56 Can't bind to 'y2' since it isn't a known property of ':svg:line'. (“t)”(dragEnd)=“onCricleDragEnd($ event)”/>] [x2] =“drawLine.x2”[y2] =“drawLine.y2”style =“stroke:rgb(255,0,0) ; stroke-width:2; stroke-dasharray:5,3“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:56无法绑定到'y2',因为它不是'的已知属性': SVG:线”。 ("icleDragEnd($event)" /> ][y2]="drawLine.y2" style="stroke:rgb(255,0,0);stroke-width:2;stroke-dasharray: 5,3" /> <"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:75 Can't bind to 'cx' since it isn't a known property of ':svg:circle'. ("="stroke:rgb(255,0,0);stroke-width:2;stroke-dasharray: 5,3" /> ][cx]="drawLine.x1" [cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="o"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@253:36 Can't bind to 'cy' since it isn't a known property of ':svg:circle'. (“icleDragEnd($ event)”/>] [y2] =“drawLine.y2”style =“stroke:rgb(255,0,0); stroke-width:2; stroke-dasharray:5,3”/> <“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@252:75无法绑定到'cx',因为它不是':svg:circle'的已知属性。(”=“stroke:rgb( 255,0,0); stroke-width:2; stroke-dasharray:5,3“/>] [cx] =”drawLine.x1“[cy] =”drawLine.y1“r =”5“stroke =”黑色“stroke-width =”3“fill =”red“(dragStart)=”o“):ng:///GlobalSettingsModule/ImageCaptureComponent.html@253:36无法绑定到'cy',因为它不是'svg:circle'的已知属性。 ("0);stroke-width:2;stroke-dasharray: 5,3" /> ][cy]="drawLine.y1" r="5" stroke="black" stroke-width="3" fill="red" (dragStart)="onCricleDragStart($e"): ng:///GlobalSettingsModule/ImageCaptureComponent.html@253:55 at syntaxError (compiler.js:486) at TemplateParser.parse (compiler.js:24674) at JitCompiler._parseTemplate (compiler.js:34629) at JitCompiler._compileTemplate (compiler.js:34604) at eval (compiler.js:34505) at Set.forEach () at JitCompiler._compileComponents (compiler.js:34505) at eval (compiler.js:34375) at Object.then (compiler.js:475) at JitCompiler._compileModuleAndComponents (compiler.js:34374) at resolvePromise (zone.js:814) at resolvePromise (zone.js:771) at eval (zone.js:873) at ZoneDelegate.invokeTask (zone.js:421) at Object.onInvokeTask (core.js:4751) at ZoneDelegate.invokeTask (zone.js:420) at Zone.runTask (zone.js:188) at drainMicroTaskQueue (zone.js:595) (“0”; stroke-width:2; stroke-dasharray:5,3“/>] [cy] =”drawLine.y1“r =”5“stroke =”black“stroke-width =”3“fill = “red”(dragStart)=“onCricleDragStart($ e”):ng:///GlobalSettingsModule/ImageCaptureComponent.html@253:55 at syntaxError(compiler.js:486)at TemplateParser.parse(compiler.js:24674)at at JitCompiler._parseTemplate(compiler.js:34629)位于eval(compiler.js:34504)的JitCompiler._compileTemplate(compiler.js:34604),位于Eval的JitCompiler._compileComponents(compiler.js:34505)的Set.forEach()处(编译器.js:34375)在Object.then(compiler.js:475)at at JitCompiler._compileModuleAndComponents(compiler.js:34374)at resolvePromise(zone.js:814)at resolvePromise(zone.js:771)at eval(zone .js:873)在ZoneDelegate.invokeTask(zone.js:421)的Zone.leInvokeTask(core.js:4751),位于Zone.runTask(zone.js:188)的ZoneDelegate.invokeTask(zone.js:420)at at drainMicroTaskQueue(zone.js:595)

You could clean this up by creating a component for your dynamic SVG. 您可以通过为动态SVG创建组件来清理它。 Use @Inputs to send in the data. 使用@Inputs发送数据。

Are your circles using a directive? 你的圈子是否使用指令? Make sure they have matching @Inputs to [cx] and [cy] . 确保它们与[cx][cy]匹配@Inputs

One way of doing would be using Elementref and modifying its attributes. 一种方法是使用Elementref并修改其属性。

Your element (in your template html): 你的元素(在你的模板html中):

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle id="circle" #circle r="20" fill="red" />
</svg>

Modify your element: 修改你的元素:

@ViewChild("circle") circle: ElementRef;
ngAfterViewInit(): void {
   this.circle.nativeElement.setAttribute("cx", this.draw.circle.cx);
   this.circle.nativeElement.setAttribute("cy", this.draw.circle.cy);
}

Please view example implementation here: https://codesandbox.io/s/angular-juzdl?fontsize=14 请在此处查看示例实现: https//codesandbox.io/s/angular-juzdl?fontize = 14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM